Git fork
at reftables-rust 3593 lines 91 kB view raw
1/* 2 * GIT - The information manager from hell 3 * 4 * Copyright (C) Linus Torvalds, 2005 5 * Copyright (C) Johannes Schindelin, 2005 6 * 7 */ 8 9#include "git-compat-util.h" 10#include "abspath.h" 11#include "advice.h" 12#include "date.h" 13#include "branch.h" 14#include "config.h" 15#include "dir.h" 16#include "parse.h" 17#include "convert.h" 18#include "environment.h" 19#include "gettext.h" 20#include "git-zlib.h" 21#include "repository.h" 22#include "lockfile.h" 23#include "exec-cmd.h" 24#include "strbuf.h" 25#include "quote.h" 26#include "hashmap.h" 27#include "string-list.h" 28#include "object-name.h" 29#include "odb.h" 30#include "path.h" 31#include "utf8.h" 32#include "color.h" 33#include "refs.h" 34#include "setup.h" 35#include "strvec.h" 36#include "trace2.h" 37#include "wildmatch.h" 38#include "write-or-die.h" 39 40struct config_source { 41 struct config_source *prev; 42 union { 43 FILE *file; 44 struct config_buf { 45 const char *buf; 46 size_t len; 47 size_t pos; 48 } buf; 49 } u; 50 enum config_origin_type origin_type; 51 const char *name; 52 enum config_error_action default_error_action; 53 int linenr; 54 int eof; 55 size_t total_len; 56 struct strbuf value; 57 struct strbuf var; 58 unsigned subsection_case_sensitive : 1; 59 60 int (*do_fgetc)(struct config_source *c); 61 int (*do_ungetc)(int c, struct config_source *conf); 62 long (*do_ftell)(struct config_source *c); 63}; 64#define CONFIG_SOURCE_INIT { 0 } 65 66/* 67 * Config that comes from trusted scopes, namely: 68 * - CONFIG_SCOPE_SYSTEM (e.g. /etc/gitconfig) 69 * - CONFIG_SCOPE_GLOBAL (e.g. $HOME/.gitconfig, $XDG_CONFIG_HOME/git) 70 * - CONFIG_SCOPE_COMMAND (e.g. "-c" option, environment variables) 71 * 72 * This is declared here for code cleanliness, but unlike the other 73 * static variables, this does not hold config parser state. 74 */ 75static struct config_set protected_config; 76 77static int config_file_fgetc(struct config_source *conf) 78{ 79 return getc_unlocked(conf->u.file); 80} 81 82static int config_file_ungetc(int c, struct config_source *conf) 83{ 84 return ungetc(c, conf->u.file); 85} 86 87static long config_file_ftell(struct config_source *conf) 88{ 89 return ftell(conf->u.file); 90} 91 92static int config_buf_fgetc(struct config_source *conf) 93{ 94 if (conf->u.buf.pos < conf->u.buf.len) 95 return conf->u.buf.buf[conf->u.buf.pos++]; 96 97 return EOF; 98} 99 100static int config_buf_ungetc(int c, struct config_source *conf) 101{ 102 if (conf->u.buf.pos > 0) { 103 conf->u.buf.pos--; 104 if (conf->u.buf.buf[conf->u.buf.pos] != c) 105 BUG("config_buf can only ungetc the same character"); 106 return c; 107 } 108 109 return EOF; 110} 111 112static long config_buf_ftell(struct config_source *conf) 113{ 114 return conf->u.buf.pos; 115} 116 117struct config_include_data { 118 int depth; 119 config_fn_t fn; 120 void *data; 121 const struct config_options *opts; 122 const struct git_config_source *config_source; 123 struct repository *repo; 124 125 /* 126 * All remote URLs discovered when reading all config files. 127 */ 128 struct string_list *remote_urls; 129}; 130#define CONFIG_INCLUDE_INIT { 0 } 131 132static int git_config_include(const char *var, const char *value, 133 const struct config_context *ctx, void *data); 134 135#define MAX_INCLUDE_DEPTH 10 136static const char include_depth_advice[] = N_( 137"exceeded maximum include depth (%d) while including\n" 138" %s\n" 139"from\n" 140" %s\n" 141"This might be due to circular includes."); 142static int handle_path_include(const struct key_value_info *kvi, 143 const char *path, 144 struct config_include_data *inc) 145{ 146 int ret = 0; 147 struct strbuf buf = STRBUF_INIT; 148 char *expanded; 149 150 if (!path) 151 return config_error_nonbool("include.path"); 152 153 expanded = interpolate_path(path, 0); 154 if (!expanded) 155 return error(_("could not expand include path '%s'"), path); 156 path = expanded; 157 158 /* 159 * Use an absolute path as-is, but interpret relative paths 160 * based on the including config file. 161 */ 162 if (!is_absolute_path(path)) { 163 char *slash; 164 165 if (!kvi || kvi->origin_type != CONFIG_ORIGIN_FILE) { 166 ret = error(_("relative config includes must come from files")); 167 goto cleanup; 168 } 169 170 slash = find_last_dir_sep(kvi->filename); 171 if (slash) 172 strbuf_add(&buf, kvi->filename, slash - kvi->filename + 1); 173 strbuf_addstr(&buf, path); 174 path = buf.buf; 175 } 176 177 if (!access_or_die(path, R_OK, 0)) { 178 if (++inc->depth > MAX_INCLUDE_DEPTH) 179 die(_(include_depth_advice), MAX_INCLUDE_DEPTH, path, 180 !kvi ? "<unknown>" : 181 kvi->filename ? kvi->filename : 182 "the command line"); 183 ret = git_config_from_file_with_options(git_config_include, path, inc, 184 kvi->scope, NULL); 185 inc->depth--; 186 } 187cleanup: 188 strbuf_release(&buf); 189 free(expanded); 190 return ret; 191} 192 193static void add_trailing_starstar_for_dir(struct strbuf *pat) 194{ 195 if (pat->len && is_dir_sep(pat->buf[pat->len - 1])) 196 strbuf_addstr(pat, "**"); 197} 198 199static int prepare_include_condition_pattern(const struct key_value_info *kvi, 200 struct strbuf *pat, 201 size_t *out) 202{ 203 struct strbuf path = STRBUF_INIT; 204 char *expanded; 205 size_t prefix = 0; 206 207 expanded = interpolate_path(pat->buf, 1); 208 if (expanded) { 209 strbuf_reset(pat); 210 strbuf_addstr(pat, expanded); 211 free(expanded); 212 } 213 214 if (pat->buf[0] == '.' && is_dir_sep(pat->buf[1])) { 215 const char *slash; 216 217 if (!kvi || kvi->origin_type != CONFIG_ORIGIN_FILE) 218 return error(_("relative config include " 219 "conditionals must come from files")); 220 221 strbuf_realpath(&path, kvi->filename, 1); 222 slash = find_last_dir_sep(path.buf); 223 if (!slash) 224 BUG("how is this possible?"); 225 strbuf_splice(pat, 0, 1, path.buf, slash - path.buf); 226 prefix = slash - path.buf + 1 /* slash */; 227 } else if (!is_absolute_path(pat->buf)) 228 strbuf_insertstr(pat, 0, "**/"); 229 230 add_trailing_starstar_for_dir(pat); 231 232 *out = prefix; 233 234 strbuf_release(&path); 235 return 0; 236} 237 238static int include_by_gitdir(const struct key_value_info *kvi, 239 const struct config_options *opts, 240 const char *cond, size_t cond_len, int icase) 241{ 242 struct strbuf text = STRBUF_INIT; 243 struct strbuf pattern = STRBUF_INIT; 244 size_t prefix; 245 int ret = 0; 246 const char *git_dir; 247 int already_tried_absolute = 0; 248 249 if (opts->git_dir) 250 git_dir = opts->git_dir; 251 else 252 goto done; 253 254 strbuf_realpath(&text, git_dir, 1); 255 strbuf_add(&pattern, cond, cond_len); 256 ret = prepare_include_condition_pattern(kvi, &pattern, &prefix); 257 if (ret < 0) 258 goto done; 259 260again: 261 if (prefix > 0) { 262 /* 263 * perform literal matching on the prefix part so that 264 * any wildcard character in it can't create side effects. 265 */ 266 if (text.len < prefix) 267 goto done; 268 if (!icase && strncmp(pattern.buf, text.buf, prefix)) 269 goto done; 270 if (icase && strncasecmp(pattern.buf, text.buf, prefix)) 271 goto done; 272 } 273 274 ret = !wildmatch(pattern.buf + prefix, text.buf + prefix, 275 WM_PATHNAME | (icase ? WM_CASEFOLD : 0)); 276 277 if (!ret && !already_tried_absolute) { 278 /* 279 * We've tried e.g. matching gitdir:~/work, but if 280 * ~/work is a symlink to /mnt/storage/work 281 * strbuf_realpath() will expand it, so the rule won't 282 * match. Let's match against a 283 * strbuf_add_absolute_path() version of the path, 284 * which'll do the right thing 285 */ 286 strbuf_reset(&text); 287 strbuf_add_absolute_path(&text, git_dir); 288 already_tried_absolute = 1; 289 goto again; 290 } 291done: 292 strbuf_release(&pattern); 293 strbuf_release(&text); 294 return ret; 295} 296 297static int include_by_branch(struct config_include_data *data, 298 const char *cond, size_t cond_len) 299{ 300 int flags; 301 int ret; 302 struct strbuf pattern = STRBUF_INIT; 303 const char *refname, *shortname; 304 305 if (!data->repo || data->repo->ref_storage_format == REF_STORAGE_FORMAT_UNKNOWN) 306 return 0; 307 308 refname = refs_resolve_ref_unsafe(get_main_ref_store(data->repo), 309 "HEAD", 0, NULL, &flags); 310 if (!refname || 311 !(flags & REF_ISSYMREF) || 312 !skip_prefix(refname, "refs/heads/", &shortname)) 313 return 0; 314 315 strbuf_add(&pattern, cond, cond_len); 316 add_trailing_starstar_for_dir(&pattern); 317 ret = !wildmatch(pattern.buf, shortname, WM_PATHNAME); 318 strbuf_release(&pattern); 319 return ret; 320} 321 322static int add_remote_url(const char *var, const char *value, 323 const struct config_context *ctx UNUSED, void *data) 324{ 325 struct string_list *remote_urls = data; 326 const char *remote_name; 327 size_t remote_name_len; 328 const char *key; 329 330 if (!parse_config_key(var, "remote", &remote_name, &remote_name_len, 331 &key) && 332 remote_name && 333 !strcmp(key, "url")) 334 string_list_append(remote_urls, value); 335 return 0; 336} 337 338static void populate_remote_urls(struct config_include_data *inc) 339{ 340 struct config_options opts; 341 342 opts = *inc->opts; 343 opts.unconditional_remote_url = 1; 344 345 inc->remote_urls = xmalloc(sizeof(*inc->remote_urls)); 346 string_list_init_dup(inc->remote_urls); 347 config_with_options(add_remote_url, inc->remote_urls, 348 inc->config_source, inc->repo, &opts); 349} 350 351static int forbid_remote_url(const char *var, const char *value UNUSED, 352 const struct config_context *ctx UNUSED, 353 void *data UNUSED) 354{ 355 const char *remote_name; 356 size_t remote_name_len; 357 const char *key; 358 359 if (!parse_config_key(var, "remote", &remote_name, &remote_name_len, 360 &key) && 361 remote_name && 362 !strcmp(key, "url")) 363 die(_("remote URLs cannot be configured in file directly or indirectly included by includeIf.hasconfig:remote.*.url")); 364 return 0; 365} 366 367static int at_least_one_url_matches_glob(const char *glob, int glob_len, 368 struct string_list *remote_urls) 369{ 370 struct strbuf pattern = STRBUF_INIT; 371 struct string_list_item *url_item; 372 int found = 0; 373 374 strbuf_add(&pattern, glob, glob_len); 375 for_each_string_list_item(url_item, remote_urls) { 376 if (!wildmatch(pattern.buf, url_item->string, WM_PATHNAME)) { 377 found = 1; 378 break; 379 } 380 } 381 strbuf_release(&pattern); 382 return found; 383} 384 385static int include_by_remote_url(struct config_include_data *inc, 386 const char *cond, size_t cond_len) 387{ 388 if (inc->opts->unconditional_remote_url) 389 return 1; 390 if (!inc->remote_urls) 391 populate_remote_urls(inc); 392 return at_least_one_url_matches_glob(cond, cond_len, 393 inc->remote_urls); 394} 395 396static int include_condition_is_true(const struct key_value_info *kvi, 397 struct config_include_data *inc, 398 const char *cond, size_t cond_len) 399{ 400 const struct config_options *opts = inc->opts; 401 402 if (skip_prefix_mem(cond, cond_len, "gitdir:", &cond, &cond_len)) 403 return include_by_gitdir(kvi, opts, cond, cond_len, 0); 404 else if (skip_prefix_mem(cond, cond_len, "gitdir/i:", &cond, &cond_len)) 405 return include_by_gitdir(kvi, opts, cond, cond_len, 1); 406 else if (skip_prefix_mem(cond, cond_len, "onbranch:", &cond, &cond_len)) 407 return include_by_branch(inc, cond, cond_len); 408 else if (skip_prefix_mem(cond, cond_len, "hasconfig:remote.*.url:", &cond, 409 &cond_len)) 410 return include_by_remote_url(inc, cond, cond_len); 411 412 /* unknown conditionals are always false */ 413 return 0; 414} 415 416static int git_config_include(const char *var, const char *value, 417 const struct config_context *ctx, 418 void *data) 419{ 420 struct config_include_data *inc = data; 421 const char *cond, *key; 422 size_t cond_len; 423 int ret; 424 425 /* 426 * Pass along all values, including "include" directives; this makes it 427 * possible to query information on the includes themselves. 428 */ 429 ret = inc->fn(var, value, ctx, inc->data); 430 if (ret < 0) 431 return ret; 432 433 if (!strcmp(var, "include.path")) 434 ret = handle_path_include(ctx->kvi, value, inc); 435 436 if (!parse_config_key(var, "includeif", &cond, &cond_len, &key) && 437 cond && include_condition_is_true(ctx->kvi, inc, cond, cond_len) && 438 !strcmp(key, "path")) { 439 config_fn_t old_fn = inc->fn; 440 441 if (inc->opts->unconditional_remote_url) 442 inc->fn = forbid_remote_url; 443 ret = handle_path_include(ctx->kvi, value, inc); 444 inc->fn = old_fn; 445 } 446 447 return ret; 448} 449 450static void git_config_push_split_parameter(const char *key, const char *value) 451{ 452 struct strbuf env = STRBUF_INIT; 453 const char *old = getenv(CONFIG_DATA_ENVIRONMENT); 454 if (old && *old) { 455 strbuf_addstr(&env, old); 456 strbuf_addch(&env, ' '); 457 } 458 sq_quote_buf(&env, key); 459 strbuf_addch(&env, '='); 460 if (value) 461 sq_quote_buf(&env, value); 462 setenv(CONFIG_DATA_ENVIRONMENT, env.buf, 1); 463 strbuf_release(&env); 464} 465 466void git_config_push_parameter(const char *text) 467{ 468 const char *value; 469 470 /* 471 * When we see: 472 * 473 * section.subsection=with=equals.key=value 474 * 475 * we cannot tell if it means: 476 * 477 * [section "subsection=with=equals"] 478 * key = value 479 * 480 * or: 481 * 482 * [section] 483 * subsection = with=equals.key=value 484 * 485 * We parse left-to-right for the first "=", meaning we'll prefer to 486 * keep the value intact over the subsection. This is historical, but 487 * also sensible since values are more likely to contain odd or 488 * untrusted input than a section name. 489 * 490 * A missing equals is explicitly allowed (as a bool-only entry). 491 */ 492 value = strchr(text, '='); 493 if (value) { 494 char *key = xmemdupz(text, value - text); 495 git_config_push_split_parameter(key, value + 1); 496 free(key); 497 } else { 498 git_config_push_split_parameter(text, NULL); 499 } 500} 501 502void git_config_push_env(const char *spec) 503{ 504 char *key; 505 const char *env_name; 506 const char *env_value; 507 508 env_name = strrchr(spec, '='); 509 if (!env_name) 510 die(_("invalid config format: %s"), spec); 511 key = xmemdupz(spec, env_name - spec); 512 env_name++; 513 if (!*env_name) 514 die(_("missing environment variable name for configuration '%.*s'"), 515 (int)(env_name - spec - 1), spec); 516 517 env_value = getenv(env_name); 518 if (!env_value) 519 die(_("missing environment variable '%s' for configuration '%.*s'"), 520 env_name, (int)(env_name - spec - 1), spec); 521 522 git_config_push_split_parameter(key, env_value); 523 free(key); 524} 525 526static inline int iskeychar(int c) 527{ 528 return isalnum(c) || c == '-'; 529} 530 531/* 532 * Auxiliary function to sanity-check and split the key into the section 533 * identifier and variable name. 534 * 535 * Returns 0 on success, -1 when there is an invalid character in the key and 536 * -2 if there is no section name in the key. 537 * 538 * store_key - pointer to char* which will hold a copy of the key with 539 * lowercase section and variable name 540 * baselen - pointer to size_t which will hold the length of the 541 * section + subsection part, can be NULL 542 */ 543int git_config_parse_key(const char *key, char **store_key, size_t *baselen_) 544{ 545 size_t i, baselen; 546 int dot; 547 const char *last_dot = strrchr(key, '.'); 548 549 /* 550 * Since "key" actually contains the section name and the real 551 * key name separated by a dot, we have to know where the dot is. 552 */ 553 554 if (last_dot == NULL || last_dot == key) { 555 error(_("key does not contain a section: %s"), key); 556 return -CONFIG_NO_SECTION_OR_NAME; 557 } 558 559 if (!last_dot[1]) { 560 error(_("key does not contain variable name: %s"), key); 561 return -CONFIG_NO_SECTION_OR_NAME; 562 } 563 564 baselen = last_dot - key; 565 if (baselen_) 566 *baselen_ = baselen; 567 568 /* 569 * Validate the key and while at it, lower case it for matching. 570 */ 571 *store_key = xmallocz(strlen(key)); 572 573 dot = 0; 574 for (i = 0; key[i]; i++) { 575 unsigned char c = key[i]; 576 if (c == '.') 577 dot = 1; 578 /* Leave the extended basename untouched.. */ 579 if (!dot || i > baselen) { 580 if (!iskeychar(c) || 581 (i == baselen + 1 && !isalpha(c))) { 582 error(_("invalid key: %s"), key); 583 goto out_free_ret_1; 584 } 585 c = tolower(c); 586 } else if (c == '\n') { 587 error(_("invalid key (newline): %s"), key); 588 goto out_free_ret_1; 589 } 590 (*store_key)[i] = c; 591 } 592 593 return 0; 594 595out_free_ret_1: 596 FREE_AND_NULL(*store_key); 597 return -CONFIG_INVALID_KEY; 598} 599 600static int config_parse_pair(const char *key, const char *value, 601 struct key_value_info *kvi, 602 config_fn_t fn, void *data) 603{ 604 char *canonical_name; 605 int ret; 606 struct config_context ctx = { 607 .kvi = kvi, 608 }; 609 610 if (!strlen(key)) 611 return error(_("empty config key")); 612 if (git_config_parse_key(key, &canonical_name, NULL)) 613 return -1; 614 615 ret = (fn(canonical_name, value, &ctx, data) < 0) ? -1 : 0; 616 free(canonical_name); 617 return ret; 618} 619 620 621/* for values read from `git_config_from_parameters()` */ 622void kvi_from_param(struct key_value_info *out) 623{ 624 out->filename = NULL; 625 out->linenr = -1; 626 out->origin_type = CONFIG_ORIGIN_CMDLINE; 627 out->scope = CONFIG_SCOPE_COMMAND; 628} 629 630int git_config_parse_parameter(const char *text, 631 config_fn_t fn, void *data) 632{ 633 const char *value; 634 struct string_list pair = STRING_LIST_INIT_DUP; 635 int ret; 636 struct key_value_info kvi = KVI_INIT; 637 638 kvi_from_param(&kvi); 639 640 string_list_split(&pair, text, "=", 1); 641 if (!pair.nr) 642 return error(_("bogus config parameter: %s"), text); 643 644 if (pair.nr == 1) 645 value = NULL; 646 else 647 value = pair.items[1].string; 648 649 if (!*pair.items[0].string) { 650 string_list_clear(&pair, 0); 651 return error(_("bogus config parameter: %s"), text); 652 } 653 654 ret = config_parse_pair(pair.items[0].string, value, &kvi, fn, data); 655 string_list_clear(&pair, 0); 656 return ret; 657} 658 659static int parse_config_env_list(char *env, struct key_value_info *kvi, 660 config_fn_t fn, void *data) 661{ 662 char *cur = env; 663 while (cur && *cur) { 664 const char *key = sq_dequote_step(cur, &cur); 665 if (!key) 666 return error(_("bogus format in %s"), 667 CONFIG_DATA_ENVIRONMENT); 668 669 if (!cur || isspace(*cur)) { 670 /* old-style 'key=value' */ 671 if (git_config_parse_parameter(key, fn, data) < 0) 672 return -1; 673 } 674 else if (*cur == '=') { 675 /* new-style 'key'='value' */ 676 const char *value; 677 678 cur++; 679 if (*cur == '\'') { 680 /* quoted value */ 681 value = sq_dequote_step(cur, &cur); 682 if (!value || (cur && !isspace(*cur))) { 683 return error(_("bogus format in %s"), 684 CONFIG_DATA_ENVIRONMENT); 685 } 686 } else if (!*cur || isspace(*cur)) { 687 /* implicit bool: 'key'= */ 688 value = NULL; 689 } else { 690 return error(_("bogus format in %s"), 691 CONFIG_DATA_ENVIRONMENT); 692 } 693 694 if (config_parse_pair(key, value, kvi, fn, data) < 0) 695 return -1; 696 } 697 else { 698 /* unknown format */ 699 return error(_("bogus format in %s"), 700 CONFIG_DATA_ENVIRONMENT); 701 } 702 703 if (cur) { 704 while (isspace(*cur)) 705 cur++; 706 } 707 } 708 return 0; 709} 710 711int git_config_from_parameters(config_fn_t fn, void *data) 712{ 713 const char *env; 714 struct strbuf envvar = STRBUF_INIT; 715 struct strvec to_free = STRVEC_INIT; 716 int ret = 0; 717 char *envw = NULL; 718 struct key_value_info kvi = KVI_INIT; 719 720 kvi_from_param(&kvi); 721 env = getenv(CONFIG_COUNT_ENVIRONMENT); 722 if (env) { 723 unsigned long count; 724 char *endp; 725 726 count = strtoul(env, &endp, 10); 727 if (*endp) { 728 ret = error(_("bogus count in %s"), CONFIG_COUNT_ENVIRONMENT); 729 goto out; 730 } 731 if (count > INT_MAX) { 732 ret = error(_("too many entries in %s"), CONFIG_COUNT_ENVIRONMENT); 733 goto out; 734 } 735 736 for (unsigned long i = 0; i < count; i++) { 737 const char *key, *value; 738 739 strbuf_addf(&envvar, "GIT_CONFIG_KEY_%lu", i); 740 key = getenv_safe(&to_free, envvar.buf); 741 if (!key) { 742 ret = error(_("missing config key %s"), envvar.buf); 743 goto out; 744 } 745 strbuf_reset(&envvar); 746 747 strbuf_addf(&envvar, "GIT_CONFIG_VALUE_%lu", i); 748 value = getenv_safe(&to_free, envvar.buf); 749 if (!value) { 750 ret = error(_("missing config value %s"), envvar.buf); 751 goto out; 752 } 753 strbuf_reset(&envvar); 754 755 if (config_parse_pair(key, value, &kvi, fn, data) < 0) { 756 ret = -1; 757 goto out; 758 } 759 } 760 } 761 762 env = getenv(CONFIG_DATA_ENVIRONMENT); 763 if (env) { 764 /* sq_dequote will write over it */ 765 envw = xstrdup(env); 766 if (parse_config_env_list(envw, &kvi, fn, data) < 0) { 767 ret = -1; 768 goto out; 769 } 770 } 771 772out: 773 strbuf_release(&envvar); 774 strvec_clear(&to_free); 775 free(envw); 776 return ret; 777} 778 779static int get_next_char(struct config_source *cs) 780{ 781 int c = cs->do_fgetc(cs); 782 783 if (c == '\r') { 784 /* DOS like systems */ 785 c = cs->do_fgetc(cs); 786 if (c != '\n') { 787 if (c != EOF) 788 cs->do_ungetc(c, cs); 789 c = '\r'; 790 } 791 } 792 793 if (c != EOF && ++cs->total_len > INT_MAX) { 794 /* 795 * This is an absurdly long config file; refuse to parse 796 * further in order to protect downstream code from integer 797 * overflows. Note that we can't return an error specifically, 798 * but we can mark EOF and put trash in the return value, 799 * which will trigger a parse error. 800 */ 801 cs->eof = 1; 802 return 0; 803 } 804 805 if (c == '\n') 806 cs->linenr++; 807 if (c == EOF) { 808 cs->eof = 1; 809 cs->linenr++; 810 c = '\n'; 811 } 812 return c; 813} 814 815static char *parse_value(struct config_source *cs) 816{ 817 int quote = 0, comment = 0; 818 size_t trim_len = 0; 819 820 strbuf_reset(&cs->value); 821 for (;;) { 822 int c = get_next_char(cs); 823 if (c == '\n') { 824 if (quote) { 825 cs->linenr--; 826 return NULL; 827 } 828 if (trim_len) 829 strbuf_setlen(&cs->value, trim_len); 830 return cs->value.buf; 831 } 832 if (comment) 833 continue; 834 if (isspace(c) && !quote) { 835 if (!trim_len) 836 trim_len = cs->value.len; 837 if (cs->value.len) 838 strbuf_addch(&cs->value, c); 839 continue; 840 } 841 if (!quote) { 842 if (c == ';' || c == '#') { 843 comment = 1; 844 continue; 845 } 846 } 847 if (trim_len) 848 trim_len = 0; 849 if (c == '\\') { 850 c = get_next_char(cs); 851 switch (c) { 852 case '\n': 853 continue; 854 case 't': 855 c = '\t'; 856 break; 857 case 'b': 858 c = '\b'; 859 break; 860 case 'n': 861 c = '\n'; 862 break; 863 /* Some characters escape as themselves */ 864 case '\\': case '"': 865 break; 866 /* Reject unknown escape sequences */ 867 default: 868 return NULL; 869 } 870 strbuf_addch(&cs->value, c); 871 continue; 872 } 873 if (c == '"') { 874 quote = 1 - quote; 875 continue; 876 } 877 strbuf_addch(&cs->value, c); 878 } 879} 880 881static int get_value(struct config_source *cs, struct key_value_info *kvi, 882 config_fn_t fn, void *data, struct strbuf *name) 883{ 884 int c; 885 char *value; 886 int ret; 887 struct config_context ctx = { 888 .kvi = kvi, 889 }; 890 891 /* Get the full name */ 892 for (;;) { 893 c = get_next_char(cs); 894 if (cs->eof) 895 break; 896 if (!iskeychar(c)) 897 break; 898 strbuf_addch(name, tolower(c)); 899 } 900 901 while (c == ' ' || c == '\t') 902 c = get_next_char(cs); 903 904 value = NULL; 905 if (c != '\n') { 906 if (c != '=') 907 return -1; 908 value = parse_value(cs); 909 if (!value) 910 return -1; 911 } 912 /* 913 * We already consumed the \n, but we need linenr to point to 914 * the line we just parsed during the call to fn to get 915 * accurate line number in error messages. 916 */ 917 cs->linenr--; 918 kvi->linenr = cs->linenr; 919 ret = fn(name->buf, value, &ctx, data); 920 if (ret >= 0) 921 cs->linenr++; 922 return ret; 923} 924 925static int get_extended_base_var(struct config_source *cs, struct strbuf *name, 926 int c) 927{ 928 cs->subsection_case_sensitive = 0; 929 do { 930 if (c == '\n') 931 goto error_incomplete_line; 932 c = get_next_char(cs); 933 } while (isspace(c)); 934 935 /* We require the format to be '[base "extension"]' */ 936 if (c != '"') 937 return -1; 938 strbuf_addch(name, '.'); 939 940 for (;;) { 941 int c = get_next_char(cs); 942 if (c == '\n') 943 goto error_incomplete_line; 944 if (c == '"') 945 break; 946 if (c == '\\') { 947 c = get_next_char(cs); 948 if (c == '\n') 949 goto error_incomplete_line; 950 } 951 strbuf_addch(name, c); 952 } 953 954 /* Final ']' */ 955 if (get_next_char(cs) != ']') 956 return -1; 957 return 0; 958error_incomplete_line: 959 cs->linenr--; 960 return -1; 961} 962 963static int get_base_var(struct config_source *cs, struct strbuf *name) 964{ 965 cs->subsection_case_sensitive = 1; 966 for (;;) { 967 int c = get_next_char(cs); 968 if (cs->eof) 969 return -1; 970 if (c == ']') 971 return 0; 972 if (isspace(c)) 973 return get_extended_base_var(cs, name, c); 974 if (!iskeychar(c) && c != '.') 975 return -1; 976 strbuf_addch(name, tolower(c)); 977 } 978} 979 980struct parse_event_data { 981 enum config_event_t previous_type; 982 size_t previous_offset; 983 const struct config_options *opts; 984}; 985 986static int do_event(struct config_source *cs, enum config_event_t type, 987 struct parse_event_data *data) 988{ 989 size_t offset; 990 991 if (!data->opts || !data->opts->event_fn) 992 return 0; 993 994 if (type == CONFIG_EVENT_WHITESPACE && 995 data->previous_type == type) 996 return 0; 997 998 offset = cs->do_ftell(cs); 999 /* 1000 * At EOF, the parser always "inserts" an extra '\n', therefore 1001 * the end offset of the event is the current file position, otherwise 1002 * we will already have advanced to the next event. 1003 */ 1004 if (type != CONFIG_EVENT_EOF) 1005 offset--; 1006 1007 if (data->previous_type != CONFIG_EVENT_EOF && 1008 data->opts->event_fn(data->previous_type, data->previous_offset, 1009 offset, cs, data->opts->event_fn_data) < 0) 1010 return -1; 1011 1012 data->previous_type = type; 1013 data->previous_offset = offset; 1014 1015 return 0; 1016} 1017 1018static void kvi_from_source(struct config_source *cs, 1019 enum config_scope scope, 1020 struct key_value_info *out) 1021{ 1022 out->filename = strintern(cs->name); 1023 out->origin_type = cs->origin_type; 1024 out->linenr = cs->linenr; 1025 out->scope = scope; 1026} 1027 1028static int git_parse_source(struct config_source *cs, config_fn_t fn, 1029 struct key_value_info *kvi, void *data, 1030 const struct config_options *opts) 1031{ 1032 int comment = 0; 1033 size_t baselen = 0; 1034 struct strbuf *var = &cs->var; 1035 int error_return = 0; 1036 char *error_msg = NULL; 1037 1038 /* U+FEFF Byte Order Mark in UTF8 */ 1039 const char *bomptr = utf8_bom; 1040 1041 /* For the parser event callback */ 1042 struct parse_event_data event_data = { 1043 CONFIG_EVENT_EOF, 0, opts 1044 }; 1045 1046 for (;;) { 1047 int c; 1048 1049 c = get_next_char(cs); 1050 if (bomptr && *bomptr) { 1051 /* We are at the file beginning; skip UTF8-encoded BOM 1052 * if present. Sane editors won't put this in on their 1053 * own, but e.g. Windows Notepad will do it happily. */ 1054 if (c == (*bomptr & 0377)) { 1055 bomptr++; 1056 continue; 1057 } else { 1058 /* Do not tolerate partial BOM. */ 1059 if (bomptr != utf8_bom) 1060 break; 1061 /* No BOM at file beginning. Cool. */ 1062 bomptr = NULL; 1063 } 1064 } 1065 if (c == '\n') { 1066 if (cs->eof) { 1067 if (do_event(cs, CONFIG_EVENT_EOF, &event_data) < 0) 1068 return -1; 1069 return 0; 1070 } 1071 if (do_event(cs, CONFIG_EVENT_WHITESPACE, &event_data) < 0) 1072 return -1; 1073 comment = 0; 1074 continue; 1075 } 1076 if (comment) 1077 continue; 1078 if (isspace(c)) { 1079 if (do_event(cs, CONFIG_EVENT_WHITESPACE, &event_data) < 0) 1080 return -1; 1081 continue; 1082 } 1083 if (c == '#' || c == ';') { 1084 if (do_event(cs, CONFIG_EVENT_COMMENT, &event_data) < 0) 1085 return -1; 1086 comment = 1; 1087 continue; 1088 } 1089 if (c == '[') { 1090 if (do_event(cs, CONFIG_EVENT_SECTION, &event_data) < 0) 1091 return -1; 1092 1093 /* Reset prior to determining a new stem */ 1094 strbuf_reset(var); 1095 if (get_base_var(cs, var) < 0 || var->len < 1) 1096 break; 1097 strbuf_addch(var, '.'); 1098 baselen = var->len; 1099 continue; 1100 } 1101 if (!isalpha(c)) 1102 break; 1103 1104 if (do_event(cs, CONFIG_EVENT_ENTRY, &event_data) < 0) 1105 return -1; 1106 1107 /* 1108 * Truncate the var name back to the section header 1109 * stem prior to grabbing the suffix part of the name 1110 * and the value. 1111 */ 1112 strbuf_setlen(var, baselen); 1113 strbuf_addch(var, tolower(c)); 1114 if (get_value(cs, kvi, fn, data, var) < 0) 1115 break; 1116 } 1117 1118 if (do_event(cs, CONFIG_EVENT_ERROR, &event_data) < 0) 1119 return -1; 1120 1121 switch (cs->origin_type) { 1122 case CONFIG_ORIGIN_BLOB: 1123 error_msg = xstrfmt(_("bad config line %d in blob %s"), 1124 cs->linenr, cs->name); 1125 break; 1126 case CONFIG_ORIGIN_FILE: 1127 error_msg = xstrfmt(_("bad config line %d in file %s"), 1128 cs->linenr, cs->name); 1129 break; 1130 case CONFIG_ORIGIN_STDIN: 1131 error_msg = xstrfmt(_("bad config line %d in standard input"), 1132 cs->linenr); 1133 break; 1134 case CONFIG_ORIGIN_SUBMODULE_BLOB: 1135 error_msg = xstrfmt(_("bad config line %d in submodule-blob %s"), 1136 cs->linenr, cs->name); 1137 break; 1138 case CONFIG_ORIGIN_CMDLINE: 1139 error_msg = xstrfmt(_("bad config line %d in command line %s"), 1140 cs->linenr, cs->name); 1141 break; 1142 default: 1143 error_msg = xstrfmt(_("bad config line %d in %s"), 1144 cs->linenr, cs->name); 1145 } 1146 1147 switch (opts && opts->error_action ? 1148 opts->error_action : 1149 cs->default_error_action) { 1150 case CONFIG_ERROR_DIE: 1151 die("%s", error_msg); 1152 break; 1153 case CONFIG_ERROR_ERROR: 1154 error_return = error("%s", error_msg); 1155 break; 1156 case CONFIG_ERROR_SILENT: 1157 error_return = -1; 1158 break; 1159 case CONFIG_ERROR_UNSET: 1160 BUG("config error action unset"); 1161 } 1162 1163 free(error_msg); 1164 return error_return; 1165} 1166 1167NORETURN 1168static void die_bad_number(const char *name, const char *value, 1169 const struct key_value_info *kvi) 1170{ 1171 const char *error_type = (errno == ERANGE) ? 1172 N_("out of range") : N_("invalid unit"); 1173 const char *bad_numeric = N_("bad numeric config value '%s' for '%s': %s"); 1174 1175 if (!kvi) 1176 BUG("kvi should not be NULL"); 1177 1178 if (!value) 1179 value = ""; 1180 1181 if (!kvi->filename) 1182 die(_(bad_numeric), value, name, _(error_type)); 1183 1184 switch (kvi->origin_type) { 1185 case CONFIG_ORIGIN_BLOB: 1186 die(_("bad numeric config value '%s' for '%s' in blob %s: %s"), 1187 value, name, kvi->filename, _(error_type)); 1188 case CONFIG_ORIGIN_FILE: 1189 die(_("bad numeric config value '%s' for '%s' in file %s: %s"), 1190 value, name, kvi->filename, _(error_type)); 1191 case CONFIG_ORIGIN_STDIN: 1192 die(_("bad numeric config value '%s' for '%s' in standard input: %s"), 1193 value, name, _(error_type)); 1194 case CONFIG_ORIGIN_SUBMODULE_BLOB: 1195 die(_("bad numeric config value '%s' for '%s' in submodule-blob %s: %s"), 1196 value, name, kvi->filename, _(error_type)); 1197 case CONFIG_ORIGIN_CMDLINE: 1198 die(_("bad numeric config value '%s' for '%s' in command line %s: %s"), 1199 value, name, kvi->filename, _(error_type)); 1200 default: 1201 die(_("bad numeric config value '%s' for '%s' in %s: %s"), 1202 value, name, kvi->filename, _(error_type)); 1203 } 1204} 1205 1206int git_config_int(const char *name, const char *value, 1207 const struct key_value_info *kvi) 1208{ 1209 int ret; 1210 if (!git_parse_int(value, &ret)) 1211 die_bad_number(name, value, kvi); 1212 return ret; 1213} 1214 1215int64_t git_config_int64(const char *name, const char *value, 1216 const struct key_value_info *kvi) 1217{ 1218 int64_t ret; 1219 if (!git_parse_int64(value, &ret)) 1220 die_bad_number(name, value, kvi); 1221 return ret; 1222} 1223 1224unsigned long git_config_ulong(const char *name, const char *value, 1225 const struct key_value_info *kvi) 1226{ 1227 unsigned long ret; 1228 if (!git_parse_ulong(value, &ret)) 1229 die_bad_number(name, value, kvi); 1230 return ret; 1231} 1232 1233ssize_t git_config_ssize_t(const char *name, const char *value, 1234 const struct key_value_info *kvi) 1235{ 1236 ssize_t ret; 1237 if (!git_parse_ssize_t(value, &ret)) 1238 die_bad_number(name, value, kvi); 1239 return ret; 1240} 1241 1242double git_config_double(const char *name, const char *value, 1243 const struct key_value_info *kvi) 1244{ 1245 double ret; 1246 if (!git_parse_double(value, &ret)) 1247 die_bad_number(name, value, kvi); 1248 return ret; 1249} 1250 1251int git_config_bool_or_int(const char *name, const char *value, 1252 const struct key_value_info *kvi, int *is_bool) 1253{ 1254 int v = git_parse_maybe_bool_text(value); 1255 if (0 <= v) { 1256 *is_bool = 1; 1257 return v; 1258 } 1259 *is_bool = 0; 1260 return git_config_int(name, value, kvi); 1261} 1262 1263int git_config_bool(const char *name, const char *value) 1264{ 1265 int v = git_parse_maybe_bool(value); 1266 if (v < 0) 1267 die(_("bad boolean config value '%s' for '%s'"), value, name); 1268 return v; 1269} 1270 1271int git_config_string(char **dest, const char *var, const char *value) 1272{ 1273 if (!value) 1274 return config_error_nonbool(var); 1275 *dest = xstrdup(value); 1276 return 0; 1277} 1278 1279int git_config_pathname(char **dest, const char *var, const char *value) 1280{ 1281 int is_optional; 1282 char *path; 1283 1284 if (!value) 1285 return config_error_nonbool(var); 1286 1287 is_optional = skip_prefix(value, ":(optional)", &value); 1288 path = interpolate_path(value, 0); 1289 if (!path) 1290 die(_("failed to expand user dir in: '%s'"), value); 1291 1292 if (is_optional && is_missing_file(path)) { 1293 free(path); 1294 return 0; 1295 } 1296 1297 *dest = path; 1298 return 0; 1299} 1300 1301int git_config_expiry_date(timestamp_t *timestamp, const char *var, const char *value) 1302{ 1303 if (!value) 1304 return config_error_nonbool(var); 1305 if (parse_expiry_date(value, timestamp)) 1306 return error(_("'%s' for '%s' is not a valid timestamp"), 1307 value, var); 1308 return 0; 1309} 1310 1311int git_config_color(char *dest, const char *var, const char *value) 1312{ 1313 if (!value) 1314 return config_error_nonbool(var); 1315 if (color_parse(value, dest) < 0) 1316 return -1; 1317 return 0; 1318} 1319 1320/* 1321 * All source specific fields in the union, die_on_error, name and the callbacks 1322 * fgetc, ungetc, ftell of top need to be initialized before calling 1323 * this function. 1324 */ 1325static int do_config_from(struct config_source *top, config_fn_t fn, 1326 void *data, enum config_scope scope, 1327 const struct config_options *opts) 1328{ 1329 struct key_value_info kvi = KVI_INIT; 1330 int ret; 1331 1332 /* push config-file parsing state stack */ 1333 top->linenr = 1; 1334 top->eof = 0; 1335 top->total_len = 0; 1336 strbuf_init(&top->value, 1024); 1337 strbuf_init(&top->var, 1024); 1338 kvi_from_source(top, scope, &kvi); 1339 1340 ret = git_parse_source(top, fn, &kvi, data, opts); 1341 1342 strbuf_release(&top->value); 1343 strbuf_release(&top->var); 1344 1345 return ret; 1346} 1347 1348static int do_config_from_file(config_fn_t fn, 1349 const enum config_origin_type origin_type, 1350 const char *name, FILE *f, void *data, 1351 enum config_scope scope, 1352 const struct config_options *opts) 1353{ 1354 struct config_source top = CONFIG_SOURCE_INIT; 1355 int ret; 1356 1357 if (origin_type == CONFIG_ORIGIN_FILE && (!name || !*name)) 1358 BUG("missing filename for CONFIG_ORIGIN_FILE"); 1359 1360 top.u.file = f; 1361 top.origin_type = origin_type; 1362 top.name = name; 1363 top.default_error_action = CONFIG_ERROR_DIE; 1364 top.do_fgetc = config_file_fgetc; 1365 top.do_ungetc = config_file_ungetc; 1366 top.do_ftell = config_file_ftell; 1367 1368 flockfile(f); 1369 ret = do_config_from(&top, fn, data, scope, opts); 1370 funlockfile(f); 1371 return ret; 1372} 1373 1374static int git_config_from_stdin(config_fn_t fn, void *data, 1375 enum config_scope scope) 1376{ 1377 return do_config_from_file(fn, CONFIG_ORIGIN_STDIN, "", stdin, data, 1378 scope, NULL); 1379} 1380 1381int git_config_from_file_with_options(config_fn_t fn, const char *filename, 1382 void *data, enum config_scope scope, 1383 const struct config_options *opts) 1384{ 1385 int ret = -1; 1386 FILE *f; 1387 1388 if (!filename) 1389 BUG("filename cannot be NULL"); 1390 f = fopen_or_warn(filename, "r"); 1391 if (f) { 1392 ret = do_config_from_file(fn, CONFIG_ORIGIN_FILE, filename, 1393 f, data, scope, opts); 1394 fclose(f); 1395 } 1396 return ret; 1397} 1398 1399int git_config_from_file(config_fn_t fn, const char *filename, void *data) 1400{ 1401 return git_config_from_file_with_options(fn, filename, data, 1402 CONFIG_SCOPE_UNKNOWN, NULL); 1403} 1404 1405int git_config_from_mem(config_fn_t fn, 1406 const enum config_origin_type origin_type, 1407 const char *name, const char *buf, size_t len, 1408 void *data, enum config_scope scope, 1409 const struct config_options *opts) 1410{ 1411 struct config_source top = CONFIG_SOURCE_INIT; 1412 1413 top.u.buf.buf = buf; 1414 top.u.buf.len = len; 1415 top.u.buf.pos = 0; 1416 top.origin_type = origin_type; 1417 top.name = name; 1418 top.default_error_action = CONFIG_ERROR_ERROR; 1419 top.do_fgetc = config_buf_fgetc; 1420 top.do_ungetc = config_buf_ungetc; 1421 top.do_ftell = config_buf_ftell; 1422 1423 return do_config_from(&top, fn, data, scope, opts); 1424} 1425 1426int git_config_from_blob_oid(config_fn_t fn, 1427 const char *name, 1428 struct repository *repo, 1429 const struct object_id *oid, 1430 void *data, 1431 enum config_scope scope) 1432{ 1433 enum object_type type; 1434 char *buf; 1435 unsigned long size; 1436 int ret; 1437 1438 buf = odb_read_object(repo->objects, oid, &type, &size); 1439 if (!buf) 1440 return error(_("unable to load config blob object '%s'"), name); 1441 if (type != OBJ_BLOB) { 1442 free(buf); 1443 return error(_("reference '%s' does not point to a blob"), name); 1444 } 1445 1446 ret = git_config_from_mem(fn, CONFIG_ORIGIN_BLOB, name, buf, size, 1447 data, scope, NULL); 1448 free(buf); 1449 1450 return ret; 1451} 1452 1453static int git_config_from_blob_ref(config_fn_t fn, 1454 struct repository *repo, 1455 const char *name, 1456 void *data, 1457 enum config_scope scope) 1458{ 1459 struct object_id oid; 1460 1461 if (repo_get_oid(repo, name, &oid) < 0) 1462 return error(_("unable to resolve config blob '%s'"), name); 1463 return git_config_from_blob_oid(fn, name, repo, &oid, data, scope); 1464} 1465 1466char *git_system_config(void) 1467{ 1468 char *system_config = xstrdup_or_null(getenv("GIT_CONFIG_SYSTEM")); 1469 if (!system_config) 1470 system_config = system_path(ETC_GITCONFIG); 1471 normalize_path_copy(system_config, system_config); 1472 return system_config; 1473} 1474 1475char *git_global_config(void) 1476{ 1477 char *user_config, *xdg_config; 1478 1479 git_global_config_paths(&user_config, &xdg_config); 1480 if (!user_config) { 1481 free(xdg_config); 1482 return NULL; 1483 } 1484 1485 if (access_or_warn(user_config, R_OK, 0) && xdg_config && 1486 !access_or_warn(xdg_config, R_OK, 0)) { 1487 free(user_config); 1488 return xdg_config; 1489 } else { 1490 free(xdg_config); 1491 return user_config; 1492 } 1493} 1494 1495void git_global_config_paths(char **user_out, char **xdg_out) 1496{ 1497 char *user_config = xstrdup_or_null(getenv("GIT_CONFIG_GLOBAL")); 1498 char *xdg_config = NULL; 1499 1500 if (!user_config) { 1501 user_config = interpolate_path("~/.gitconfig", 0); 1502 xdg_config = xdg_config_home("config"); 1503 } 1504 1505 *user_out = user_config; 1506 *xdg_out = xdg_config; 1507} 1508 1509int git_config_system(void) 1510{ 1511 return !git_env_bool("GIT_CONFIG_NOSYSTEM", 0); 1512} 1513 1514static int do_git_config_sequence(const struct config_options *opts, 1515 const struct repository *repo, 1516 config_fn_t fn, void *data) 1517{ 1518 int ret = 0; 1519 char *system_config = git_system_config(); 1520 char *xdg_config = NULL; 1521 char *user_config = NULL; 1522 char *repo_config; 1523 char *worktree_config; 1524 1525 /* 1526 * Ensure that either: 1527 * - the git_dir and commondir are both set, or 1528 * - the git_dir and commondir are both NULL 1529 */ 1530 if (!opts->git_dir != !opts->commondir) 1531 BUG("only one of commondir and git_dir is non-NULL"); 1532 1533 if (opts->commondir) { 1534 repo_config = mkpathdup("%s/config", opts->commondir); 1535 worktree_config = mkpathdup("%s/config.worktree", opts->git_dir); 1536 } else { 1537 repo_config = NULL; 1538 worktree_config = NULL; 1539 } 1540 1541 if (git_config_system() && system_config && 1542 !access_or_die(system_config, R_OK, 1543 opts->system_gently ? ACCESS_EACCES_OK : 0)) 1544 ret += git_config_from_file_with_options(fn, system_config, 1545 data, CONFIG_SCOPE_SYSTEM, 1546 NULL); 1547 1548 git_global_config_paths(&user_config, &xdg_config); 1549 1550 if (xdg_config && !access_or_die(xdg_config, R_OK, ACCESS_EACCES_OK)) 1551 ret += git_config_from_file_with_options(fn, xdg_config, data, 1552 CONFIG_SCOPE_GLOBAL, NULL); 1553 1554 if (user_config && !access_or_die(user_config, R_OK, ACCESS_EACCES_OK)) 1555 ret += git_config_from_file_with_options(fn, user_config, data, 1556 CONFIG_SCOPE_GLOBAL, NULL); 1557 1558 if (!opts->ignore_repo && repo_config && 1559 !access_or_die(repo_config, R_OK, 0)) 1560 ret += git_config_from_file_with_options(fn, repo_config, data, 1561 CONFIG_SCOPE_LOCAL, NULL); 1562 1563 if (!opts->ignore_worktree && worktree_config && 1564 repo && repo->repository_format_worktree_config && 1565 !access_or_die(worktree_config, R_OK, 0)) { 1566 ret += git_config_from_file_with_options(fn, worktree_config, data, 1567 CONFIG_SCOPE_WORKTREE, 1568 NULL); 1569 } 1570 1571 if (!opts->ignore_cmdline && git_config_from_parameters(fn, data) < 0) 1572 die(_("unable to parse command-line config")); 1573 1574 free(system_config); 1575 free(xdg_config); 1576 free(user_config); 1577 free(repo_config); 1578 free(worktree_config); 1579 return ret; 1580} 1581 1582int config_with_options(config_fn_t fn, void *data, 1583 const struct git_config_source *config_source, 1584 struct repository *repo, 1585 const struct config_options *opts) 1586{ 1587 struct config_include_data inc = CONFIG_INCLUDE_INIT; 1588 int ret; 1589 1590 if (opts->respect_includes) { 1591 inc.fn = fn; 1592 inc.data = data; 1593 inc.opts = opts; 1594 inc.repo = repo; 1595 inc.config_source = config_source; 1596 fn = git_config_include; 1597 data = &inc; 1598 } 1599 1600 /* 1601 * If we have a specific filename, use it. Otherwise, follow the 1602 * regular lookup sequence. 1603 */ 1604 if (config_source && config_source->use_stdin) { 1605 ret = git_config_from_stdin(fn, data, config_source->scope); 1606 } else if (config_source && config_source->file) { 1607 ret = git_config_from_file_with_options(fn, config_source->file, 1608 data, config_source->scope, 1609 NULL); 1610 } else if (config_source && config_source->blob) { 1611 ret = git_config_from_blob_ref(fn, repo, config_source->blob, 1612 data, config_source->scope); 1613 } else { 1614 ret = do_git_config_sequence(opts, repo, fn, data); 1615 } 1616 1617 if (inc.remote_urls) { 1618 string_list_clear(inc.remote_urls, 0); 1619 FREE_AND_NULL(inc.remote_urls); 1620 } 1621 return ret; 1622} 1623 1624static void configset_iter(struct config_set *set, config_fn_t fn, void *data) 1625{ 1626 int value_index; 1627 struct string_list *values; 1628 struct config_set_element *entry; 1629 struct configset_list *list = &set->list; 1630 struct config_context ctx = CONFIG_CONTEXT_INIT; 1631 1632 for (size_t i = 0; i < list->nr; i++) { 1633 entry = list->items[i].e; 1634 value_index = list->items[i].value_index; 1635 values = &entry->value_list; 1636 1637 ctx.kvi = values->items[value_index].util; 1638 if (fn(entry->key, values->items[value_index].string, &ctx, data) < 0) 1639 git_die_config_linenr(entry->key, 1640 ctx.kvi->filename, 1641 ctx.kvi->linenr); 1642 } 1643} 1644 1645void read_early_config(struct repository *repo, config_fn_t cb, void *data) 1646{ 1647 struct config_options opts = {0}; 1648 struct strbuf commondir = STRBUF_INIT; 1649 struct strbuf gitdir = STRBUF_INIT; 1650 1651 opts.respect_includes = 1; 1652 1653 if (repo && repo->gitdir) { 1654 opts.commondir = repo_get_common_dir(repo); 1655 opts.git_dir = repo_get_git_dir(repo); 1656 /* 1657 * When setup_git_directory() was not yet asked to discover the 1658 * GIT_DIR, we ask discover_git_directory() to figure out whether there 1659 * is any repository config we should use (but unlike 1660 * setup_git_directory_gently(), no global state is changed, most 1661 * notably, the current working directory is still the same after the 1662 * call). 1663 */ 1664 } else if (!discover_git_directory(&commondir, &gitdir)) { 1665 opts.commondir = commondir.buf; 1666 opts.git_dir = gitdir.buf; 1667 } 1668 1669 config_with_options(cb, data, NULL, NULL, &opts); 1670 1671 strbuf_release(&commondir); 1672 strbuf_release(&gitdir); 1673} 1674 1675void read_very_early_config(config_fn_t cb, void *data) 1676{ 1677 struct config_options opts = { 0 }; 1678 1679 opts.respect_includes = 1; 1680 opts.ignore_repo = 1; 1681 opts.ignore_worktree = 1; 1682 opts.ignore_cmdline = 1; 1683 opts.system_gently = 1; 1684 1685 config_with_options(cb, data, NULL, NULL, &opts); 1686} 1687 1688RESULT_MUST_BE_USED 1689static int configset_find_element(struct config_set *set, const char *key, 1690 struct config_set_element **dest) 1691{ 1692 struct config_set_element k; 1693 struct config_set_element *found_entry; 1694 char *normalized_key; 1695 int ret; 1696 1697 /* 1698 * `key` may come from the user, so normalize it before using it 1699 * for querying entries from the hashmap. 1700 */ 1701 ret = git_config_parse_key(key, &normalized_key, NULL); 1702 if (ret) 1703 return ret; 1704 1705 hashmap_entry_init(&k.ent, strhash(normalized_key)); 1706 k.key = normalized_key; 1707 found_entry = hashmap_get_entry(&set->config_hash, &k, ent, NULL); 1708 free(normalized_key); 1709 *dest = found_entry; 1710 return 0; 1711} 1712 1713static int configset_add_value(const struct key_value_info *kvi_p, 1714 struct config_set *set, const char *key, 1715 const char *value) 1716{ 1717 struct config_set_element *e; 1718 struct string_list_item *si; 1719 struct configset_list_item *l_item; 1720 struct key_value_info *kv_info = xmalloc(sizeof(*kv_info)); 1721 int ret; 1722 1723 ret = configset_find_element(set, key, &e); 1724 if (ret) 1725 return ret; 1726 /* 1727 * Since the keys are being fed by git_config*() callback mechanism, they 1728 * are already normalized. So simply add them without any further munging. 1729 */ 1730 if (!e) { 1731 e = xmalloc(sizeof(*e)); 1732 hashmap_entry_init(&e->ent, strhash(key)); 1733 e->key = xstrdup(key); 1734 string_list_init_dup(&e->value_list); 1735 hashmap_add(&set->config_hash, &e->ent); 1736 } 1737 si = string_list_append_nodup(&e->value_list, xstrdup_or_null(value)); 1738 1739 ALLOC_GROW(set->list.items, set->list.nr + 1, set->list.alloc); 1740 l_item = &set->list.items[set->list.nr++]; 1741 l_item->e = e; 1742 l_item->value_index = e->value_list.nr - 1; 1743 1744 *kv_info = *kvi_p; 1745 si->util = kv_info; 1746 1747 return 0; 1748} 1749 1750static int config_set_element_cmp(const void *cmp_data UNUSED, 1751 const struct hashmap_entry *eptr, 1752 const struct hashmap_entry *entry_or_key, 1753 const void *keydata UNUSED) 1754{ 1755 const struct config_set_element *e1, *e2; 1756 1757 e1 = container_of(eptr, const struct config_set_element, ent); 1758 e2 = container_of(entry_or_key, const struct config_set_element, ent); 1759 1760 return strcmp(e1->key, e2->key); 1761} 1762 1763void git_configset_init(struct config_set *set) 1764{ 1765 hashmap_init(&set->config_hash, config_set_element_cmp, NULL, 0); 1766 set->hash_initialized = 1; 1767 set->list.nr = 0; 1768 set->list.alloc = 0; 1769 set->list.items = NULL; 1770} 1771 1772void git_configset_clear(struct config_set *set) 1773{ 1774 struct config_set_element *entry; 1775 struct hashmap_iter iter; 1776 if (!set->hash_initialized) 1777 return; 1778 1779 hashmap_for_each_entry(&set->config_hash, &iter, entry, 1780 ent /* member name */) { 1781 free(entry->key); 1782 string_list_clear(&entry->value_list, 1); 1783 } 1784 hashmap_clear_and_free(&set->config_hash, struct config_set_element, ent); 1785 set->hash_initialized = 0; 1786 free(set->list.items); 1787 set->list.nr = 0; 1788 set->list.alloc = 0; 1789 set->list.items = NULL; 1790} 1791 1792static int config_set_callback(const char *key, const char *value, 1793 const struct config_context *ctx, 1794 void *cb) 1795{ 1796 struct config_set *set = cb; 1797 configset_add_value(ctx->kvi, set, key, value); 1798 return 0; 1799} 1800 1801int git_configset_add_file(struct config_set *set, const char *filename) 1802{ 1803 return git_config_from_file(config_set_callback, filename, set); 1804} 1805 1806int git_configset_get_value(struct config_set *set, const char *key, 1807 const char **value, struct key_value_info *kvi) 1808{ 1809 const struct string_list *values = NULL; 1810 int ret; 1811 struct string_list_item item; 1812 /* 1813 * Follows "last one wins" semantic, i.e., if there are multiple matches for the 1814 * queried key in the files of the configset, the value returned will be the last 1815 * value in the value list for that key. 1816 */ 1817 if ((ret = git_configset_get_value_multi(set, key, &values))) 1818 return ret; 1819 1820 assert(values->nr > 0); 1821 item = values->items[values->nr - 1]; 1822 *value = item.string; 1823 if (kvi) 1824 *kvi = *((struct key_value_info *)item.util); 1825 return 0; 1826} 1827 1828int git_configset_get_value_multi(struct config_set *set, const char *key, 1829 const struct string_list **dest) 1830{ 1831 struct config_set_element *e; 1832 int ret; 1833 1834 if ((ret = configset_find_element(set, key, &e))) 1835 return ret; 1836 else if (!e) 1837 return 1; 1838 *dest = &e->value_list; 1839 1840 return 0; 1841} 1842 1843static int check_multi_string(struct string_list_item *item, void *util) 1844{ 1845 return item->string ? 0 : config_error_nonbool(util); 1846} 1847 1848int git_configset_get_string_multi(struct config_set *cs, const char *key, 1849 const struct string_list **dest) 1850{ 1851 int ret; 1852 1853 if ((ret = git_configset_get_value_multi(cs, key, dest))) 1854 return ret; 1855 if ((ret = for_each_string_list((struct string_list *)*dest, 1856 check_multi_string, (void *)key))) 1857 return ret; 1858 1859 return 0; 1860} 1861 1862int git_configset_get(struct config_set *set, const char *key) 1863{ 1864 struct config_set_element *e; 1865 int ret; 1866 1867 if ((ret = configset_find_element(set, key, &e))) 1868 return ret; 1869 else if (!e) 1870 return 1; 1871 return 0; 1872} 1873 1874int git_configset_get_string(struct config_set *set, const char *key, char **dest) 1875{ 1876 const char *value; 1877 if (!git_configset_get_value(set, key, &value, NULL)) 1878 return git_config_string(dest, key, value); 1879 else 1880 return 1; 1881} 1882 1883static int git_configset_get_string_tmp(struct config_set *set, const char *key, 1884 const char **dest) 1885{ 1886 const char *value; 1887 if (!git_configset_get_value(set, key, &value, NULL)) { 1888 if (!value) 1889 return config_error_nonbool(key); 1890 *dest = value; 1891 return 0; 1892 } else { 1893 return 1; 1894 } 1895} 1896 1897int git_configset_get_int(struct config_set *set, const char *key, int *dest) 1898{ 1899 const char *value; 1900 struct key_value_info kvi; 1901 1902 if (!git_configset_get_value(set, key, &value, &kvi)) { 1903 *dest = git_config_int(key, value, &kvi); 1904 return 0; 1905 } else 1906 return 1; 1907} 1908 1909int git_configset_get_ulong(struct config_set *set, const char *key, unsigned long *dest) 1910{ 1911 const char *value; 1912 struct key_value_info kvi; 1913 1914 if (!git_configset_get_value(set, key, &value, &kvi)) { 1915 *dest = git_config_ulong(key, value, &kvi); 1916 return 0; 1917 } else 1918 return 1; 1919} 1920 1921int git_configset_get_bool(struct config_set *set, const char *key, int *dest) 1922{ 1923 const char *value; 1924 if (!git_configset_get_value(set, key, &value, NULL)) { 1925 *dest = git_config_bool(key, value); 1926 return 0; 1927 } else 1928 return 1; 1929} 1930 1931int git_configset_get_bool_or_int(struct config_set *set, const char *key, 1932 int *is_bool, int *dest) 1933{ 1934 const char *value; 1935 struct key_value_info kvi; 1936 1937 if (!git_configset_get_value(set, key, &value, &kvi)) { 1938 *dest = git_config_bool_or_int(key, value, &kvi, is_bool); 1939 return 0; 1940 } else 1941 return 1; 1942} 1943 1944int git_configset_get_maybe_bool(struct config_set *set, const char *key, int *dest) 1945{ 1946 const char *value; 1947 if (!git_configset_get_value(set, key, &value, NULL)) { 1948 *dest = git_parse_maybe_bool(value); 1949 if (*dest == -1) 1950 return -1; 1951 return 0; 1952 } else 1953 return 1; 1954} 1955 1956int git_configset_get_pathname(struct config_set *set, const char *key, char **dest) 1957{ 1958 const char *value; 1959 if (!git_configset_get_value(set, key, &value, NULL)) 1960 return git_config_pathname(dest, key, value); 1961 else 1962 return 1; 1963} 1964 1965struct comment_char_config { 1966 unsigned last_key_id; 1967 bool auto_set; 1968 bool auto_set_in_file; 1969 struct strintmap key_flags; 1970 size_t alloc, nr; 1971 struct comment_char_config_item { 1972 unsigned key_id; 1973 char *path; 1974 enum config_scope scope; 1975 } *item; 1976}; 1977 1978#define COMMENT_CHAR_CFG_INIT { \ 1979 .key_flags = STRINTMAP_INIT, \ 1980 } 1981 1982static void comment_char_config_release(struct comment_char_config *config) 1983{ 1984 strintmap_clear(&config->key_flags); 1985 for (size_t i = 0; i < config->nr; i++) 1986 free(config->item[i].path); 1987 free(config->item); 1988} 1989 1990/* Used to track whether the key occurs more than once in a given file */ 1991#define KEY_SEEN_ONCE 1u 1992#define KEY_SEEN_TWICE 2u 1993#define COMMENT_KEY_SHIFT(id) (2 * (id)) 1994#define COMMENT_KEY_MASK(id) (3u << COMMENT_KEY_SHIFT(id)) 1995 1996static void set_comment_key_flags(struct comment_char_config *config, 1997 const char *path, unsigned id, unsigned value) 1998{ 1999 unsigned old = strintmap_get(&config->key_flags, path); 2000 unsigned new = (old & ~COMMENT_KEY_MASK(id)) | 2001 value << COMMENT_KEY_SHIFT(id); 2002 2003 strintmap_set(&config->key_flags, path, new); 2004} 2005 2006static unsigned get_comment_key_flags(struct comment_char_config *config, 2007 const char *path, unsigned id) 2008{ 2009 unsigned value = strintmap_get(&config->key_flags, path); 2010 2011 return (value & COMMENT_KEY_MASK(id)) >> COMMENT_KEY_SHIFT(id); 2012} 2013 2014static const char *comment_key_name(unsigned id) 2015{ 2016 static const char *name[] = { 2017 "core.commentChar", 2018 "core.commentString", 2019 }; 2020 2021 if (id >= ARRAY_SIZE(name)) 2022 BUG("invalid comment key id"); 2023 2024 return name[id]; 2025} 2026 2027static void comment_char_callback(const char *key, const char *value, 2028 const struct config_context *ctx, void *data) 2029{ 2030 struct comment_char_config *config = data; 2031 const struct key_value_info *kvi = ctx->kvi; 2032 unsigned key_id; 2033 2034 if (!strcmp(key, "core.commentchar")) 2035 key_id = 0; 2036 else if (!strcmp(key, "core.commentstring")) 2037 key_id = 1; 2038 else 2039 return; 2040 2041 config->last_key_id = key_id; 2042 config->auto_set = value && !strcmp(value, "auto"); 2043 if (kvi->origin_type != CONFIG_ORIGIN_FILE) { 2044 return; 2045 } else if (get_comment_key_flags(config, kvi->filename, key_id)) { 2046 set_comment_key_flags(config, kvi->filename, key_id, 2047 KEY_SEEN_TWICE); 2048 } else { 2049 struct comment_char_config_item *item; 2050 2051 ALLOC_GROW_BY(config->item, config->nr, 1, config->alloc); 2052 item = &config->item[config->nr - 1]; 2053 item->key_id = key_id; 2054 item->scope = kvi->scope; 2055 item->path = xstrdup(kvi->filename); 2056 set_comment_key_flags(config, kvi->filename, key_id, 2057 KEY_SEEN_ONCE); 2058 } 2059 config->auto_set_in_file = config->auto_set; 2060} 2061 2062static void add_config_scope_arg(struct repository *repo, struct strbuf *buf, 2063 struct comment_char_config_item *item) 2064{ 2065 char *global_config = git_global_config(); 2066 char *system_config = git_system_config(); 2067 2068 if (item->scope == CONFIG_SCOPE_SYSTEM && access(item->path, W_OK)) { 2069 /* 2070 * If the user cannot write to the system config recommend 2071 * setting the global config instead. 2072 */ 2073 strbuf_addstr(buf, "--global "); 2074 } else if (fspatheq(item->path, system_config)) { 2075 strbuf_addstr(buf, "--system "); 2076 } else if (fspatheq(item->path, global_config)) { 2077 strbuf_addstr(buf, "--global "); 2078 } else if (fspatheq(item->path, 2079 mkpath("%s/config", 2080 repo_get_git_dir(repo)))) { 2081 ; /* --local is the default */ 2082 } else if (fspatheq(item->path, 2083 mkpath("%s/config.worktree", 2084 repo_get_common_dir(repo)))) { 2085 strbuf_addstr(buf, "--worktree "); 2086 } else { 2087 const char *path = item->path; 2088 const char *home = getenv("HOME"); 2089 2090 strbuf_addstr(buf, "--file "); 2091 if (home && !fspathncmp(path, home, strlen(home))) { 2092 path += strlen(home); 2093 if (!fspathncmp(path, "/", 1)) 2094 path++; 2095 strbuf_addstr(buf, "~/"); 2096 } 2097 sq_quote_buf_pretty(buf, path); 2098 strbuf_addch(buf, ' '); 2099 } 2100 2101 free(global_config); 2102 free(system_config); 2103} 2104 2105static bool can_unset_comment_char_config(struct comment_char_config *config) 2106{ 2107 for (size_t i = 0; i < config->nr; i++) { 2108 struct comment_char_config_item *item = &config->item[i]; 2109 2110 if (item->scope == CONFIG_SCOPE_SYSTEM && 2111 access(item->path, W_OK)) 2112 return false; 2113 } 2114 2115 return true; 2116} 2117 2118static void add_unset_auto_comment_char_advice(struct repository *repo, 2119 struct comment_char_config *config) 2120{ 2121 struct strbuf buf = STRBUF_INIT; 2122 2123 if (!can_unset_comment_char_config(config)) 2124 return; 2125 2126 for (size_t i = 0; i < config->nr; i++) { 2127 struct comment_char_config_item *item = &config->item[i]; 2128 2129 strbuf_addstr(&buf, " git config unset "); 2130 add_config_scope_arg(repo, &buf, item); 2131 if (get_comment_key_flags(config, item->path, item->key_id) == KEY_SEEN_TWICE) 2132 strbuf_addstr(&buf, "--all "); 2133 strbuf_addf(&buf, "%s\n", comment_key_name(item->key_id)); 2134 } 2135 advise(_("\nTo use the default comment string (#) please run\n\n%s"), 2136 buf.buf); 2137 strbuf_release(&buf); 2138} 2139 2140static void add_comment_char_advice(struct repository *repo, 2141 struct comment_char_config *config) 2142{ 2143 struct strbuf buf = STRBUF_INIT; 2144 struct comment_char_config_item *item; 2145 /* TRANSLATORS this is a place holder for the value of core.commentString */ 2146 const char *placeholder = _("<comment string>"); 2147 2148 /* 2149 * If auto is set in the last file that we saw advise the user how to 2150 * update their config. 2151 */ 2152 if (!config->auto_set_in_file) 2153 return; 2154 2155 add_unset_auto_comment_char_advice(repo, config); 2156 item = &config->item[config->nr - 1]; 2157 strbuf_reset(&buf); 2158 strbuf_addstr(&buf, " git config set "); 2159 add_config_scope_arg(repo, &buf, item); 2160 strbuf_addf(&buf, "%s %s\n", comment_key_name(item->key_id), 2161 placeholder); 2162 advise(_("\nTo set a custom comment string please run\n\n" 2163 "%s\nwhere '%s' is the string you wish to use.\n"), 2164 buf.buf, placeholder); 2165 strbuf_release(&buf); 2166} 2167 2168#undef KEY_SEEN_ONCE 2169#undef KEY_SEEN_TWICE 2170#undef COMMENT_KEY_SHIFT 2171#undef COMMENT_KEY_MASK 2172 2173struct repo_config { 2174 struct repository *repo; 2175 struct comment_char_config comment_char_config; 2176}; 2177 2178#define REPO_CONFIG_INIT(repo_) { \ 2179 .comment_char_config = COMMENT_CHAR_CFG_INIT, \ 2180 .repo = repo_, \ 2181 }; 2182 2183static void repo_config_release(struct repo_config *config) 2184{ 2185 comment_char_config_release(&config->comment_char_config); 2186} 2187 2188#ifdef WITH_BREAKING_CHANGES 2189static void check_auto_comment_char_config(struct repository *repo, 2190 struct comment_char_config *config) 2191{ 2192 if (!config->auto_set) 2193 return; 2194 2195 die_message(_("Support for '%s=auto' has been removed in Git 3.0"), 2196 comment_key_name(config->last_key_id)); 2197 add_comment_char_advice(repo, config); 2198 die(NULL); 2199} 2200#else 2201static void check_auto_comment_char_config(struct repository *repo, 2202 struct comment_char_config *config) 2203{ 2204 extern bool warn_on_auto_comment_char; 2205 const char *DEPRECATED_CONFIG_ENV = 2206 "GIT_AUTO_COMMENT_CHAR_CONFIG_WARNING_GIVEN"; 2207 2208 if (!config->auto_set || !warn_on_auto_comment_char) 2209 return; 2210 2211 /* 2212 * Use an environment variable to ensure that subprocesses do not repeat 2213 * the warning. 2214 */ 2215 if (git_env_bool(DEPRECATED_CONFIG_ENV, false)) 2216 return; 2217 2218 setenv(DEPRECATED_CONFIG_ENV, "true", true); 2219 2220 warning(_("Support for '%s=auto' is deprecated and will be removed in " 2221 "Git 3.0"), comment_key_name(config->last_key_id)); 2222 add_comment_char_advice(repo, config); 2223} 2224#endif /* WITH_BREAKING_CHANGES */ 2225 2226static void check_deprecated_config(struct repo_config *config) 2227{ 2228 if (!config->repo->check_deprecated_config) 2229 return; 2230 2231 check_auto_comment_char_config(config->repo, 2232 &config->comment_char_config); 2233} 2234 2235static int repo_config_callback(const char *key, const char *value, 2236 const struct config_context *ctx, void *data) 2237{ 2238 struct repo_config *config = data; 2239 2240 comment_char_callback(key, value, ctx, &config->comment_char_config); 2241 return config_set_callback(key, value, ctx, config->repo->config); 2242} 2243 2244/* Functions use to read configuration from a repository */ 2245static void repo_read_config(struct repository *repo) 2246{ 2247 struct config_options opts = { 0 }; 2248 struct repo_config config = REPO_CONFIG_INIT(repo); 2249 2250 opts.respect_includes = 1; 2251 opts.commondir = repo->commondir; 2252 opts.git_dir = repo->gitdir; 2253 2254 if (!repo->config) 2255 CALLOC_ARRAY(repo->config, 1); 2256 else 2257 git_configset_clear(repo->config); 2258 2259 git_configset_init(repo->config); 2260 if (config_with_options(repo_config_callback, &config, NULL, repo, 2261 &opts) < 0) 2262 /* 2263 * config_with_options() normally returns only 2264 * zero, as most errors are fatal, and 2265 * non-fatal potential errors are guarded by "if" 2266 * statements that are entered only when no error is 2267 * possible. 2268 * 2269 * If we ever encounter a non-fatal error, it means 2270 * something went really wrong and we should stop 2271 * immediately. 2272 */ 2273 die(_("unknown error occurred while reading the configuration files")); 2274 check_deprecated_config(&config); 2275 repo_config_release(&config); 2276} 2277 2278static void git_config_check_init(struct repository *repo) 2279{ 2280 if (repo->config && repo->config->hash_initialized) 2281 return; 2282 repo_read_config(repo); 2283} 2284 2285void repo_config_clear(struct repository *repo) 2286{ 2287 if (!repo->config || !repo->config->hash_initialized) 2288 return; 2289 git_configset_clear(repo->config); 2290} 2291 2292void repo_config(struct repository *repo, config_fn_t fn, void *data) 2293{ 2294 if (!repo) { 2295 read_very_early_config(fn, data); 2296 return; 2297 } 2298 git_config_check_init(repo); 2299 configset_iter(repo->config, fn, data); 2300} 2301 2302int repo_config_get(struct repository *repo, const char *key) 2303{ 2304 git_config_check_init(repo); 2305 return git_configset_get(repo->config, key); 2306} 2307 2308int repo_config_get_value(struct repository *repo, 2309 const char *key, const char **value) 2310{ 2311 git_config_check_init(repo); 2312 return git_configset_get_value(repo->config, key, value, NULL); 2313} 2314 2315int repo_config_get_value_multi(struct repository *repo, const char *key, 2316 const struct string_list **dest) 2317{ 2318 git_config_check_init(repo); 2319 return git_configset_get_value_multi(repo->config, key, dest); 2320} 2321 2322int repo_config_get_string_multi(struct repository *repo, const char *key, 2323 const struct string_list **dest) 2324{ 2325 git_config_check_init(repo); 2326 return git_configset_get_string_multi(repo->config, key, dest); 2327} 2328 2329int repo_config_get_string(struct repository *repo, 2330 const char *key, char **dest) 2331{ 2332 int ret; 2333 git_config_check_init(repo); 2334 ret = git_configset_get_string(repo->config, key, dest); 2335 if (ret < 0) 2336 git_die_config(repo, key, NULL); 2337 return ret; 2338} 2339 2340int repo_config_get_string_tmp(struct repository *repo, 2341 const char *key, const char **dest) 2342{ 2343 int ret; 2344 git_config_check_init(repo); 2345 ret = git_configset_get_string_tmp(repo->config, key, dest); 2346 if (ret < 0) 2347 git_die_config(repo, key, NULL); 2348 return ret; 2349} 2350 2351int repo_config_get_int(struct repository *repo, 2352 const char *key, int *dest) 2353{ 2354 git_config_check_init(repo); 2355 return git_configset_get_int(repo->config, key, dest); 2356} 2357 2358int repo_config_get_ulong(struct repository *repo, 2359 const char *key, unsigned long *dest) 2360{ 2361 git_config_check_init(repo); 2362 return git_configset_get_ulong(repo->config, key, dest); 2363} 2364 2365int repo_config_get_bool(struct repository *repo, 2366 const char *key, int *dest) 2367{ 2368 git_config_check_init(repo); 2369 return git_configset_get_bool(repo->config, key, dest); 2370} 2371 2372int repo_config_get_bool_or_int(struct repository *repo, 2373 const char *key, int *is_bool, int *dest) 2374{ 2375 git_config_check_init(repo); 2376 return git_configset_get_bool_or_int(repo->config, key, is_bool, dest); 2377} 2378 2379int repo_config_get_maybe_bool(struct repository *repo, 2380 const char *key, int *dest) 2381{ 2382 git_config_check_init(repo); 2383 return git_configset_get_maybe_bool(repo->config, key, dest); 2384} 2385 2386int repo_config_get_pathname(struct repository *repo, 2387 const char *key, char **dest) 2388{ 2389 int ret; 2390 git_config_check_init(repo); 2391 ret = git_configset_get_pathname(repo->config, key, dest); 2392 if (ret < 0) 2393 git_die_config(repo, key, NULL); 2394 return ret; 2395} 2396 2397/* Read values into protected_config. */ 2398static void read_protected_config(void) 2399{ 2400 struct config_options opts = { 2401 .respect_includes = 1, 2402 .ignore_repo = 1, 2403 .ignore_worktree = 1, 2404 .system_gently = 1, 2405 }; 2406 2407 git_configset_init(&protected_config); 2408 config_with_options(config_set_callback, &protected_config, NULL, 2409 NULL, &opts); 2410} 2411 2412void git_protected_config(config_fn_t fn, void *data) 2413{ 2414 if (!protected_config.hash_initialized) 2415 read_protected_config(); 2416 configset_iter(&protected_config, fn, data); 2417} 2418 2419int repo_config_get_expiry(struct repository *r, const char *key, char **output) 2420{ 2421 int ret = repo_config_get_string(r, key, output); 2422 2423 if (ret) 2424 return ret; 2425 if (strcmp(*output, "now")) { 2426 timestamp_t now = approxidate("now"); 2427 if (approxidate(*output) >= now) 2428 git_die_config(r, key, _("Invalid %s: '%s'"), key, *output); 2429 } 2430 return ret; 2431} 2432 2433int repo_config_get_expiry_in_days(struct repository *r, const char *key, 2434 timestamp_t *expiry, timestamp_t now) 2435{ 2436 const char *expiry_string; 2437 intmax_t days; 2438 timestamp_t when; 2439 2440 if (repo_config_get_string_tmp(r, key, &expiry_string)) 2441 return 1; /* no such thing */ 2442 2443 if (git_parse_signed(expiry_string, &days, maximum_signed_value_of_type(int))) { 2444 const int scale = 86400; 2445 *expiry = now - days * scale; 2446 return 0; 2447 } 2448 2449 if (!parse_expiry_date(expiry_string, &when)) { 2450 *expiry = when; 2451 return 0; 2452 } 2453 return -1; /* thing exists but cannot be parsed */ 2454} 2455 2456int repo_config_get_split_index(struct repository *r) 2457{ 2458 int val; 2459 2460 if (!repo_config_get_maybe_bool(r, "core.splitindex", &val)) 2461 return val; 2462 2463 return -1; /* default value */ 2464} 2465 2466int repo_config_get_max_percent_split_change(struct repository *r) 2467{ 2468 int val = -1; 2469 2470 if (!repo_config_get_int(r, "splitindex.maxpercentchange", &val)) { 2471 if (0 <= val && val <= 100) 2472 return val; 2473 2474 return error(_("splitIndex.maxPercentChange value '%d' " 2475 "should be between 0 and 100"), val); 2476 } 2477 2478 return -1; /* default value */ 2479} 2480 2481int repo_config_get_index_threads(struct repository *r, int *dest) 2482{ 2483 int is_bool, val; 2484 2485 val = git_env_ulong("GIT_TEST_INDEX_THREADS", 0); 2486 if (val) { 2487 *dest = val; 2488 return 0; 2489 } 2490 2491 if (!repo_config_get_bool_or_int(r, "index.threads", &is_bool, &val)) { 2492 if (is_bool) 2493 *dest = val ? 0 : 1; 2494 else 2495 *dest = val; 2496 return 0; 2497 } 2498 2499 return 1; 2500} 2501 2502NORETURN 2503void git_die_config_linenr(const char *key, const char *filename, int linenr) 2504{ 2505 if (!filename) 2506 die(_("unable to parse '%s' from command-line config"), key); 2507 else 2508 die(_("bad config variable '%s' in file '%s' at line %d"), 2509 key, filename, linenr); 2510} 2511 2512void git_die_config(struct repository *r, const char *key, const char *err, ...) 2513{ 2514 const struct string_list *values; 2515 struct key_value_info *kv_info; 2516 report_fn error_fn = get_error_routine(); 2517 2518 if (err) { 2519 va_list params; 2520 va_start(params, err); 2521 error_fn(err, params); 2522 va_end(params); 2523 } 2524 if (repo_config_get_value_multi(r, key, &values)) 2525 BUG("for key '%s' we must have a value to report on", key); 2526 kv_info = values->items[values->nr - 1].util; 2527 git_die_config_linenr(key, kv_info->filename, kv_info->linenr); 2528} 2529 2530/* 2531 * Find all the stuff for repo_config_set() below. 2532 */ 2533 2534struct config_store_data { 2535 size_t baselen; 2536 char *key; 2537 int do_not_match; 2538 const char *fixed_value; 2539 regex_t *value_pattern; 2540 int multi_replace; 2541 struct { 2542 size_t begin, end; 2543 enum config_event_t type; 2544 int is_keys_section; 2545 } *parsed; 2546 unsigned int parsed_nr, parsed_alloc, *seen, seen_nr, seen_alloc; 2547 unsigned int key_seen:1, section_seen:1, is_keys_section:1; 2548}; 2549#define CONFIG_STORE_INIT { 0 } 2550 2551static void config_store_data_clear(struct config_store_data *store) 2552{ 2553 free(store->key); 2554 if (store->value_pattern != NULL && 2555 store->value_pattern != CONFIG_REGEX_NONE) { 2556 regfree(store->value_pattern); 2557 free(store->value_pattern); 2558 } 2559 free(store->parsed); 2560 free(store->seen); 2561 memset(store, 0, sizeof(*store)); 2562} 2563 2564static int matches(const char *key, const char *value, 2565 const struct config_store_data *store) 2566{ 2567 if (strcmp(key, store->key)) 2568 return 0; /* not ours */ 2569 if (store->fixed_value && value) 2570 return !strcmp(store->fixed_value, value); 2571 if (!store->value_pattern) 2572 return 1; /* always matches */ 2573 if (store->value_pattern == CONFIG_REGEX_NONE) 2574 return 0; /* never matches */ 2575 2576 return store->do_not_match ^ 2577 (value && !regexec(store->value_pattern, value, 0, NULL, 0)); 2578} 2579 2580static int store_aux_event(enum config_event_t type, size_t begin, size_t end, 2581 struct config_source *cs, void *data) 2582{ 2583 struct config_store_data *store = data; 2584 2585 ALLOC_GROW(store->parsed, store->parsed_nr + 1, store->parsed_alloc); 2586 store->parsed[store->parsed_nr].begin = begin; 2587 store->parsed[store->parsed_nr].end = end; 2588 store->parsed[store->parsed_nr].type = type; 2589 2590 if (type == CONFIG_EVENT_SECTION) { 2591 int (*cmpfn)(const char *, const char *, size_t); 2592 2593 if (cs->var.len < 2 || cs->var.buf[cs->var.len - 1] != '.') 2594 return error(_("invalid section name '%s'"), cs->var.buf); 2595 2596 if (cs->subsection_case_sensitive) 2597 cmpfn = strncasecmp; 2598 else 2599 cmpfn = strncmp; 2600 2601 /* Is this the section we were looking for? */ 2602 store->is_keys_section = 2603 store->parsed[store->parsed_nr].is_keys_section = 2604 cs->var.len - 1 == store->baselen && 2605 !cmpfn(cs->var.buf, store->key, store->baselen); 2606 if (store->is_keys_section) { 2607 store->section_seen = 1; 2608 ALLOC_GROW(store->seen, store->seen_nr + 1, 2609 store->seen_alloc); 2610 store->seen[store->seen_nr] = store->parsed_nr; 2611 } 2612 } 2613 2614 store->parsed_nr++; 2615 2616 return 0; 2617} 2618 2619static int store_aux(const char *key, const char *value, 2620 const struct config_context *ctx UNUSED, void *cb) 2621{ 2622 struct config_store_data *store = cb; 2623 2624 if (store->key_seen) { 2625 if (matches(key, value, store)) { 2626 if (store->seen_nr == 1 && store->multi_replace == 0) { 2627 warning(_("%s has multiple values"), key); 2628 } 2629 2630 ALLOC_GROW(store->seen, store->seen_nr + 1, 2631 store->seen_alloc); 2632 2633 store->seen[store->seen_nr] = store->parsed_nr; 2634 store->seen_nr++; 2635 } 2636 } else if (store->is_keys_section) { 2637 /* 2638 * Do not increment matches yet: this may not be a match, but we 2639 * are in the desired section. 2640 */ 2641 ALLOC_GROW(store->seen, store->seen_nr + 1, store->seen_alloc); 2642 store->seen[store->seen_nr] = store->parsed_nr; 2643 store->section_seen = 1; 2644 2645 if (matches(key, value, store)) { 2646 store->seen_nr++; 2647 store->key_seen = 1; 2648 } 2649 } 2650 2651 return 0; 2652} 2653 2654static int write_error(const char *filename) 2655{ 2656 error(_("failed to write new configuration file %s"), filename); 2657 2658 /* Same error code as "failed to rename". */ 2659 return 4; 2660} 2661 2662static struct strbuf store_create_section(const char *key, 2663 const struct config_store_data *store) 2664{ 2665 const char *dot; 2666 size_t i; 2667 struct strbuf sb = STRBUF_INIT; 2668 2669 dot = memchr(key, '.', store->baselen); 2670 if (dot) { 2671 strbuf_addf(&sb, "[%.*s \"", (int)(dot - key), key); 2672 for (i = dot - key + 1; i < store->baselen; i++) { 2673 if (key[i] == '"' || key[i] == '\\') 2674 strbuf_addch(&sb, '\\'); 2675 strbuf_addch(&sb, key[i]); 2676 } 2677 strbuf_addstr(&sb, "\"]\n"); 2678 } else { 2679 strbuf_addch(&sb, '['); 2680 strbuf_add(&sb, key, store->baselen); 2681 strbuf_addstr(&sb, "]\n"); 2682 } 2683 2684 return sb; 2685} 2686 2687static ssize_t write_section(int fd, const char *key, 2688 const struct config_store_data *store) 2689{ 2690 struct strbuf sb = store_create_section(key, store); 2691 ssize_t ret; 2692 2693 ret = write_in_full(fd, sb.buf, sb.len); 2694 strbuf_release(&sb); 2695 2696 return ret; 2697} 2698 2699static ssize_t write_pair(int fd, const char *key, const char *value, 2700 const char *comment, 2701 const struct config_store_data *store) 2702{ 2703 int i; 2704 ssize_t ret; 2705 const char *quote = ""; 2706 struct strbuf sb = STRBUF_INIT; 2707 2708 /* 2709 * Check to see if the value needs to be surrounded with a dq pair. 2710 * Note that problematic characters are always backslash-quoted; this 2711 * check is about not losing leading or trailing SP and strings that 2712 * follow beginning-of-comment characters (i.e. ';' and '#') by the 2713 * configuration parser. 2714 */ 2715 if (value[0] == ' ') 2716 quote = "\""; 2717 for (i = 0; value[i]; i++) 2718 if (value[i] == ';' || value[i] == '#' || value[i] == '\r') 2719 quote = "\""; 2720 if (i && value[i - 1] == ' ') 2721 quote = "\""; 2722 2723 strbuf_addf(&sb, "\t%s = %s", key + store->baselen + 1, quote); 2724 2725 for (i = 0; value[i]; i++) 2726 switch (value[i]) { 2727 case '\n': 2728 strbuf_addstr(&sb, "\\n"); 2729 break; 2730 case '\t': 2731 strbuf_addstr(&sb, "\\t"); 2732 break; 2733 case '"': 2734 case '\\': 2735 strbuf_addch(&sb, '\\'); 2736 /* fallthrough */ 2737 default: 2738 strbuf_addch(&sb, value[i]); 2739 break; 2740 } 2741 2742 if (comment) 2743 strbuf_addf(&sb, "%s%s\n", quote, comment); 2744 else 2745 strbuf_addf(&sb, "%s\n", quote); 2746 2747 ret = write_in_full(fd, sb.buf, sb.len); 2748 strbuf_release(&sb); 2749 2750 return ret; 2751} 2752 2753/* 2754 * If we are about to unset the last key(s) in a section, and if there are 2755 * no comments surrounding (or included in) the section, we will want to 2756 * extend begin/end to remove the entire section. 2757 * 2758 * Note: the parameter `seen_ptr` points to the index into the store.seen 2759 * array. * This index may be incremented if a section has more than one 2760 * entry (which all are to be removed). 2761 */ 2762static void maybe_remove_section(struct config_store_data *store, 2763 size_t *begin_offset, size_t *end_offset, 2764 unsigned *seen_ptr) 2765{ 2766 size_t begin; 2767 int section_seen = 0; 2768 unsigned int i, seen; 2769 2770 /* 2771 * First, ensure that this is the first key, and that there are no 2772 * comments before the entry nor before the section header. 2773 */ 2774 seen = *seen_ptr; 2775 for (i = store->seen[seen]; i > 0; i--) { 2776 enum config_event_t type = store->parsed[i - 1].type; 2777 2778 if (type == CONFIG_EVENT_COMMENT) 2779 /* There is a comment before this entry or section */ 2780 return; 2781 if (type == CONFIG_EVENT_ENTRY) { 2782 if (!section_seen) 2783 /* This is not the section's first entry. */ 2784 return; 2785 /* We encountered no comment before the section. */ 2786 break; 2787 } 2788 if (type == CONFIG_EVENT_SECTION) { 2789 if (!store->parsed[i - 1].is_keys_section) 2790 break; 2791 section_seen = 1; 2792 } 2793 } 2794 begin = store->parsed[i].begin; 2795 2796 /* 2797 * Next, make sure that we are removing the last key(s) in the section, 2798 * and that there are no comments that are possibly about the current 2799 * section. 2800 */ 2801 for (i = store->seen[seen] + 1; i < store->parsed_nr; i++) { 2802 enum config_event_t type = store->parsed[i].type; 2803 2804 if (type == CONFIG_EVENT_COMMENT) 2805 return; 2806 if (type == CONFIG_EVENT_SECTION) { 2807 if (store->parsed[i].is_keys_section) 2808 continue; 2809 break; 2810 } 2811 if (type == CONFIG_EVENT_ENTRY) { 2812 if (++seen < store->seen_nr && 2813 i == store->seen[seen]) 2814 /* We want to remove this entry, too */ 2815 continue; 2816 /* There is another entry in this section. */ 2817 return; 2818 } 2819 } 2820 2821 /* 2822 * We are really removing the last entry/entries from this section, and 2823 * there are no enclosed or surrounding comments. Remove the entire, 2824 * now-empty section. 2825 */ 2826 *seen_ptr = seen; 2827 *begin_offset = begin; 2828 if (i < store->parsed_nr) 2829 *end_offset = store->parsed[i].begin; 2830 else 2831 *end_offset = store->parsed[store->parsed_nr - 1].end; 2832} 2833 2834int repo_config_set_in_file_gently(struct repository *r, const char *config_filename, 2835 const char *key, const char *comment, const char *value) 2836{ 2837 return repo_config_set_multivar_in_file_gently(r, config_filename, key, value, NULL, comment, 0); 2838} 2839 2840void repo_config_set_in_file(struct repository *r, const char *config_filename, 2841 const char *key, const char *value) 2842{ 2843 repo_config_set_multivar_in_file(r, config_filename, key, value, NULL, 0); 2844} 2845 2846int repo_config_set_gently(struct repository *r, const char *key, const char *value) 2847{ 2848 return repo_config_set_multivar_gently(r, key, value, NULL, 0); 2849} 2850 2851int repo_config_set_worktree_gently(struct repository *r, 2852 const char *key, const char *value) 2853{ 2854 /* Only use worktree-specific config if it is already enabled. */ 2855 if (r->repository_format_worktree_config) { 2856 char *file = repo_git_path(r, "config.worktree"); 2857 int ret = repo_config_set_multivar_in_file_gently( 2858 r, file, key, value, NULL, NULL, 0); 2859 free(file); 2860 return ret; 2861 } 2862 return repo_config_set_multivar_gently(r, key, value, NULL, 0); 2863} 2864 2865void repo_config_set(struct repository *r, const char *key, const char *value) 2866{ 2867 repo_config_set_multivar(r, key, value, NULL, 0); 2868 2869 trace2_cmd_set_config(key, value); 2870} 2871 2872char *git_config_prepare_comment_string(const char *comment) 2873{ 2874 size_t leading_blanks; 2875 char *prepared; 2876 2877 if (!comment) 2878 return NULL; 2879 2880 if (strchr(comment, '\n')) 2881 die(_("no multi-line comment allowed: '%s'"), comment); 2882 2883 /* 2884 * If it begins with one or more leading whitespace characters 2885 * followed by '#", the comment string is used as-is. 2886 * 2887 * If it begins with '#', a SP is inserted between the comment 2888 * and the value the comment is about. 2889 * 2890 * Otherwise, the value is followed by a SP followed by '#' 2891 * followed by SP and then the comment string comes. 2892 */ 2893 2894 leading_blanks = strspn(comment, " \t"); 2895 if (leading_blanks && comment[leading_blanks] == '#') 2896 prepared = xstrdup(comment); /* use it as-is */ 2897 else if (comment[0] == '#') 2898 prepared = xstrfmt(" %s", comment); 2899 else 2900 prepared = xstrfmt(" # %s", comment); 2901 2902 return prepared; 2903} 2904 2905static void validate_comment_string(const char *comment) 2906{ 2907 size_t leading_blanks; 2908 2909 if (!comment) 2910 return; 2911 /* 2912 * The front-end must have massaged the comment string 2913 * properly before calling us. 2914 */ 2915 if (strchr(comment, '\n')) 2916 BUG("multi-line comments are not permitted: '%s'", comment); 2917 2918 leading_blanks = strspn(comment, " \t"); 2919 if (!leading_blanks || comment[leading_blanks] != '#') 2920 BUG("comment must begin with one or more SP followed by '#': '%s'", 2921 comment); 2922} 2923 2924/* 2925 * If value==NULL, unset in (remove from) config, 2926 * if value_pattern!=NULL, disregard key/value pairs where value does not match. 2927 * if value_pattern==CONFIG_REGEX_NONE, do not match any existing values 2928 * (only add a new one) 2929 * if flags contains the CONFIG_FLAGS_MULTI_REPLACE flag, all matching 2930 * key/values are removed before a single new pair is written. If the 2931 * flag is not present, then replace only the first match. 2932 * 2933 * Returns 0 on success. 2934 * 2935 * This function does this: 2936 * 2937 * - it locks the config file by creating ".git/config.lock" 2938 * 2939 * - it then parses the config using store_aux() as validator to find 2940 * the position on the key/value pair to replace. If it is to be unset, 2941 * it must be found exactly once. 2942 * 2943 * - the config file is mmap()ed and the part before the match (if any) is 2944 * written to the lock file, then the changed part and the rest. 2945 * 2946 * - the config file is removed and the lock file rename()d to it. 2947 * 2948 */ 2949int repo_config_set_multivar_in_file_gently(struct repository *r, 2950 const char *config_filename, 2951 const char *key, const char *value, 2952 const char *value_pattern, 2953 const char *comment, 2954 unsigned flags) 2955{ 2956 int fd = -1, in_fd = -1; 2957 int ret; 2958 struct lock_file lock = LOCK_INIT; 2959 char *filename_buf = NULL; 2960 char *contents = NULL; 2961 size_t contents_sz; 2962 struct config_store_data store = CONFIG_STORE_INIT; 2963 bool saved_check_deprecated_config = r->check_deprecated_config; 2964 2965 /* 2966 * Do not warn or die if there are deprecated config settings as 2967 * we want the user to be able to change those settings by running 2968 * "git config". 2969 */ 2970 r->check_deprecated_config = false; 2971 2972 validate_comment_string(comment); 2973 2974 /* parse-key returns negative; flip the sign to feed exit(3) */ 2975 ret = 0 - git_config_parse_key(key, &store.key, &store.baselen); 2976 if (ret) 2977 goto out_free; 2978 2979 store.multi_replace = (flags & CONFIG_FLAGS_MULTI_REPLACE) != 0; 2980 2981 if (!config_filename) 2982 config_filename = filename_buf = repo_git_path(r, "config"); 2983 2984 /* 2985 * The lock serves a purpose in addition to locking: the new 2986 * contents of .git/config will be written into it. 2987 */ 2988 fd = hold_lock_file_for_update(&lock, config_filename, 0); 2989 if (fd < 0) { 2990 error_errno(_("could not lock config file %s"), config_filename); 2991 ret = CONFIG_NO_LOCK; 2992 goto out_free; 2993 } 2994 2995 /* 2996 * If .git/config does not exist yet, write a minimal version. 2997 */ 2998 in_fd = open(config_filename, O_RDONLY); 2999 if ( in_fd < 0 ) { 3000 if ( ENOENT != errno ) { 3001 error_errno(_("opening %s"), config_filename); 3002 ret = CONFIG_INVALID_FILE; /* same as "invalid config file" */ 3003 goto out_free; 3004 } 3005 /* if nothing to unset, error out */ 3006 if (!value) { 3007 ret = CONFIG_NOTHING_SET; 3008 goto out_free; 3009 } 3010 3011 free(store.key); 3012 store.key = xstrdup(key); 3013 if (write_section(fd, key, &store) < 0 || 3014 write_pair(fd, key, value, comment, &store) < 0) 3015 goto write_err_out; 3016 } else { 3017 struct stat st; 3018 size_t copy_begin, copy_end; 3019 unsigned i; 3020 int new_line = 0; 3021 struct config_options opts; 3022 3023 if (!value_pattern) 3024 store.value_pattern = NULL; 3025 else if (value_pattern == CONFIG_REGEX_NONE) 3026 store.value_pattern = CONFIG_REGEX_NONE; 3027 else if (flags & CONFIG_FLAGS_FIXED_VALUE) 3028 store.fixed_value = value_pattern; 3029 else { 3030 if (value_pattern[0] == '!') { 3031 store.do_not_match = 1; 3032 value_pattern++; 3033 } else 3034 store.do_not_match = 0; 3035 3036 store.value_pattern = (regex_t*)xmalloc(sizeof(regex_t)); 3037 if (regcomp(store.value_pattern, value_pattern, 3038 REG_EXTENDED)) { 3039 error(_("invalid pattern: %s"), value_pattern); 3040 FREE_AND_NULL(store.value_pattern); 3041 ret = CONFIG_INVALID_PATTERN; 3042 goto out_free; 3043 } 3044 } 3045 3046 ALLOC_GROW(store.parsed, 1, store.parsed_alloc); 3047 store.parsed[0].end = 0; 3048 3049 memset(&opts, 0, sizeof(opts)); 3050 opts.event_fn = store_aux_event; 3051 opts.event_fn_data = &store; 3052 3053 /* 3054 * After this, store.parsed will contain offsets of all the 3055 * parsed elements, and store.seen will contain a list of 3056 * matches, as indices into store.parsed. 3057 * 3058 * As a side effect, we make sure to transform only a valid 3059 * existing config file. 3060 */ 3061 if (git_config_from_file_with_options(store_aux, 3062 config_filename, 3063 &store, CONFIG_SCOPE_UNKNOWN, 3064 &opts)) { 3065 error(_("invalid config file %s"), config_filename); 3066 ret = CONFIG_INVALID_FILE; 3067 goto out_free; 3068 } 3069 3070 /* if nothing to unset, or too many matches, error out */ 3071 if ((store.seen_nr == 0 && value == NULL) || 3072 (store.seen_nr > 1 && !store.multi_replace)) { 3073 ret = CONFIG_NOTHING_SET; 3074 goto out_free; 3075 } 3076 3077 if (fstat(in_fd, &st) == -1) { 3078 error_errno(_("fstat on %s failed"), config_filename); 3079 ret = CONFIG_INVALID_FILE; 3080 goto out_free; 3081 } 3082 3083 contents_sz = xsize_t(st.st_size); 3084 contents = xmmap_gently(NULL, contents_sz, PROT_READ, 3085 MAP_PRIVATE, in_fd, 0); 3086 if (contents == MAP_FAILED) { 3087 if (errno == ENODEV && S_ISDIR(st.st_mode)) 3088 errno = EISDIR; 3089 error_errno(_("unable to mmap '%s'%s"), 3090 config_filename, mmap_os_err()); 3091 ret = CONFIG_INVALID_FILE; 3092 contents = NULL; 3093 goto out_free; 3094 } 3095 close(in_fd); 3096 in_fd = -1; 3097 3098 if (chmod(get_lock_file_path(&lock), st.st_mode & 07777) < 0) { 3099 error_errno(_("chmod on %s failed"), get_lock_file_path(&lock)); 3100 ret = CONFIG_NO_WRITE; 3101 goto out_free; 3102 } 3103 3104 if (store.seen_nr == 0) { 3105 if (!store.seen_alloc) { 3106 /* Did not see key nor section */ 3107 ALLOC_GROW(store.seen, 1, store.seen_alloc); 3108 store.seen[0] = store.parsed_nr 3109 - !!store.parsed_nr; 3110 } 3111 store.seen_nr = 1; 3112 } 3113 3114 for (i = 0, copy_begin = 0; i < store.seen_nr; i++) { 3115 size_t replace_end; 3116 int j = store.seen[i]; 3117 3118 new_line = 0; 3119 if (!store.key_seen) { 3120 copy_end = store.parsed[j].end; 3121 /* include '\n' when copying section header */ 3122 if (copy_end > 0 && copy_end < contents_sz && 3123 contents[copy_end - 1] != '\n' && 3124 contents[copy_end] == '\n') 3125 copy_end++; 3126 replace_end = copy_end; 3127 } else { 3128 replace_end = store.parsed[j].end; 3129 copy_end = store.parsed[j].begin; 3130 if (!value) 3131 maybe_remove_section(&store, 3132 &copy_end, 3133 &replace_end, &i); 3134 /* 3135 * Swallow preceding white-space on the same 3136 * line. 3137 */ 3138 while (copy_end > 0 ) { 3139 char c = contents[copy_end - 1]; 3140 3141 if (isspace(c) && c != '\n') 3142 copy_end--; 3143 else 3144 break; 3145 } 3146 } 3147 3148 if (copy_end > 0 && contents[copy_end-1] != '\n') 3149 new_line = 1; 3150 3151 /* write the first part of the config */ 3152 if (copy_end > copy_begin) { 3153 if (write_in_full(fd, contents + copy_begin, 3154 copy_end - copy_begin) < 0) 3155 goto write_err_out; 3156 if (new_line && 3157 write_str_in_full(fd, "\n") < 0) 3158 goto write_err_out; 3159 } 3160 copy_begin = replace_end; 3161 } 3162 3163 /* write the pair (value == NULL means unset) */ 3164 if (value) { 3165 if (!store.section_seen) { 3166 if (write_section(fd, key, &store) < 0) 3167 goto write_err_out; 3168 } 3169 if (write_pair(fd, key, value, comment, &store) < 0) 3170 goto write_err_out; 3171 } 3172 3173 /* write the rest of the config */ 3174 if (copy_begin < contents_sz) 3175 if (write_in_full(fd, contents + copy_begin, 3176 contents_sz - copy_begin) < 0) 3177 goto write_err_out; 3178 3179 munmap(contents, contents_sz); 3180 contents = NULL; 3181 } 3182 3183 if (commit_lock_file(&lock) < 0) { 3184 error_errno(_("could not write config file %s"), config_filename); 3185 ret = CONFIG_NO_WRITE; 3186 goto out_free; 3187 } 3188 3189 ret = 0; 3190 3191 /* Invalidate the config cache */ 3192 repo_config_clear(r); 3193 3194out_free: 3195 rollback_lock_file(&lock); 3196 free(filename_buf); 3197 if (contents) 3198 munmap(contents, contents_sz); 3199 if (in_fd >= 0) 3200 close(in_fd); 3201 config_store_data_clear(&store); 3202 r->check_deprecated_config = saved_check_deprecated_config; 3203 return ret; 3204 3205write_err_out: 3206 ret = write_error(get_lock_file_path(&lock)); 3207 goto out_free; 3208} 3209 3210void repo_config_set_multivar_in_file(struct repository *r, 3211 const char *config_filename, 3212 const char *key, const char *value, 3213 const char *value_pattern, unsigned flags) 3214{ 3215 if (!repo_config_set_multivar_in_file_gently(r, config_filename, key, value, 3216 value_pattern, NULL, flags)) 3217 return; 3218 if (value) 3219 die(_("could not set '%s' to '%s'"), key, value); 3220 else 3221 die(_("could not unset '%s'"), key); 3222} 3223 3224int repo_config_set_multivar_gently(struct repository *r, const char *key, 3225 const char *value, 3226 const char *value_pattern, unsigned flags) 3227{ 3228 char *file = repo_git_path(r, "config"); 3229 int res = repo_config_set_multivar_in_file_gently(r, file, 3230 key, value, 3231 value_pattern, 3232 NULL, flags); 3233 free(file); 3234 return res; 3235} 3236 3237void repo_config_set_multivar(struct repository *r, 3238 const char *key, const char *value, 3239 const char *value_pattern, unsigned flags) 3240{ 3241 char *file = repo_git_path(r, "config"); 3242 repo_config_set_multivar_in_file(r, file, key, value, 3243 value_pattern, flags); 3244 free(file); 3245} 3246 3247static size_t section_name_match (const char *buf, const char *name) 3248{ 3249 size_t i = 0, j = 0; 3250 int dot = 0; 3251 if (buf[i] != '[') 3252 return 0; 3253 for (i = 1; buf[i] && buf[i] != ']'; i++) { 3254 if (!dot && isspace(buf[i])) { 3255 dot = 1; 3256 if (name[j++] != '.') 3257 break; 3258 for (i++; isspace(buf[i]); i++) 3259 ; /* do nothing */ 3260 if (buf[i] != '"') 3261 break; 3262 continue; 3263 } 3264 if (buf[i] == '\\' && dot) 3265 i++; 3266 else if (buf[i] == '"' && dot) { 3267 for (i++; isspace(buf[i]); i++) 3268 ; /* do_nothing */ 3269 break; 3270 } 3271 if (buf[i] != name[j++]) 3272 break; 3273 } 3274 if (buf[i] == ']' && name[j] == 0) { 3275 /* 3276 * We match, now just find the right length offset by 3277 * gobbling up any whitespace after it, as well 3278 */ 3279 i++; 3280 for (; buf[i] && isspace(buf[i]); i++) 3281 ; /* do nothing */ 3282 return i; 3283 } 3284 return 0; 3285} 3286 3287static int section_name_is_ok(const char *name) 3288{ 3289 /* Empty section names are bogus. */ 3290 if (!*name) 3291 return 0; 3292 3293 /* 3294 * Before a dot, we must be alphanumeric or dash. After the first dot, 3295 * anything goes, so we can stop checking. 3296 */ 3297 for (; *name && *name != '.'; name++) 3298 if (*name != '-' && !isalnum(*name)) 3299 return 0; 3300 return 1; 3301} 3302 3303#define GIT_CONFIG_MAX_LINE_LEN (512 * 1024) 3304 3305/* if new_name == NULL, the section is removed instead */ 3306static int repo_config_copy_or_rename_section_in_file( 3307 struct repository *r, 3308 const char *config_filename, 3309 const char *old_name, 3310 const char *new_name, int copy) 3311{ 3312 int ret = 0, remove = 0; 3313 char *filename_buf = NULL; 3314 struct lock_file lock = LOCK_INIT; 3315 int out_fd; 3316 struct strbuf buf = STRBUF_INIT; 3317 FILE *config_file = NULL; 3318 struct stat st; 3319 struct strbuf copystr = STRBUF_INIT; 3320 struct config_store_data store; 3321 uint32_t line_nr = 0; 3322 3323 memset(&store, 0, sizeof(store)); 3324 3325 if (new_name && !section_name_is_ok(new_name)) { 3326 ret = error(_("invalid section name: %s"), new_name); 3327 goto out_no_rollback; 3328 } 3329 3330 if (!config_filename) 3331 config_filename = filename_buf = repo_git_path(r, "config"); 3332 3333 out_fd = hold_lock_file_for_update(&lock, config_filename, 0); 3334 if (out_fd < 0) { 3335 ret = error(_("could not lock config file %s"), config_filename); 3336 goto out; 3337 } 3338 3339 if (!(config_file = fopen(config_filename, "rb"))) { 3340 ret = warn_on_fopen_errors(config_filename); 3341 if (ret) 3342 goto out; 3343 /* no config file means nothing to rename, no error */ 3344 goto commit_and_out; 3345 } 3346 3347 if (fstat(fileno(config_file), &st) == -1) { 3348 ret = error_errno(_("fstat on %s failed"), config_filename); 3349 goto out; 3350 } 3351 3352 if (chmod(get_lock_file_path(&lock), st.st_mode & 07777) < 0) { 3353 ret = error_errno(_("chmod on %s failed"), 3354 get_lock_file_path(&lock)); 3355 goto out; 3356 } 3357 3358 while (!strbuf_getwholeline(&buf, config_file, '\n')) { 3359 size_t i, length; 3360 int is_section = 0; 3361 char *output = buf.buf; 3362 3363 line_nr++; 3364 3365 if (buf.len >= GIT_CONFIG_MAX_LINE_LEN) { 3366 ret = error(_("refusing to work with overly long line " 3367 "in '%s' on line %"PRIuMAX), 3368 config_filename, (uintmax_t)line_nr); 3369 goto out; 3370 } 3371 3372 for (i = 0; buf.buf[i] && isspace(buf.buf[i]); i++) 3373 ; /* do nothing */ 3374 if (buf.buf[i] == '[') { 3375 /* it's a section */ 3376 size_t offset; 3377 is_section = 1; 3378 3379 /* 3380 * When encountering a new section under -c we 3381 * need to flush out any section we're already 3382 * coping and begin anew. There might be 3383 * multiple [branch "$name"] sections. 3384 */ 3385 if (copystr.len > 0) { 3386 if (write_in_full(out_fd, copystr.buf, copystr.len) < 0) { 3387 ret = write_error(get_lock_file_path(&lock)); 3388 goto out; 3389 } 3390 strbuf_reset(&copystr); 3391 } 3392 3393 offset = section_name_match(&buf.buf[i], old_name); 3394 if (offset > 0) { 3395 ret++; 3396 if (!new_name) { 3397 remove = 1; 3398 continue; 3399 } 3400 store.baselen = strlen(new_name); 3401 if (!copy) { 3402 if (write_section(out_fd, new_name, &store) < 0) { 3403 ret = write_error(get_lock_file_path(&lock)); 3404 goto out; 3405 } 3406 /* 3407 * We wrote out the new section, with 3408 * a newline, now skip the old 3409 * section's length 3410 */ 3411 output += offset + i; 3412 if (strlen(output) > 0) { 3413 /* 3414 * More content means there's 3415 * a declaration to put on the 3416 * next line; indent with a 3417 * tab 3418 */ 3419 output -= 1; 3420 output[0] = '\t'; 3421 } 3422 } else { 3423 strbuf_release(&copystr); 3424 copystr = store_create_section(new_name, &store); 3425 } 3426 } 3427 remove = 0; 3428 } 3429 if (remove) 3430 continue; 3431 length = strlen(output); 3432 3433 if (!is_section && copystr.len > 0) { 3434 strbuf_add(&copystr, output, length); 3435 } 3436 3437 if (write_in_full(out_fd, output, length) < 0) { 3438 ret = write_error(get_lock_file_path(&lock)); 3439 goto out; 3440 } 3441 } 3442 3443 /* 3444 * Copy a trailing section at the end of the config, won't be 3445 * flushed by the usual "flush because we have a new section 3446 * logic in the loop above. 3447 */ 3448 if (copystr.len > 0) { 3449 if (write_in_full(out_fd, copystr.buf, copystr.len) < 0) { 3450 ret = write_error(get_lock_file_path(&lock)); 3451 goto out; 3452 } 3453 strbuf_reset(&copystr); 3454 } 3455 3456 fclose(config_file); 3457 config_file = NULL; 3458commit_and_out: 3459 if (commit_lock_file(&lock) < 0) 3460 ret = error_errno(_("could not write config file %s"), 3461 config_filename); 3462out: 3463 if (config_file) 3464 fclose(config_file); 3465 rollback_lock_file(&lock); 3466out_no_rollback: 3467 free(filename_buf); 3468 config_store_data_clear(&store); 3469 strbuf_release(&buf); 3470 strbuf_release(&copystr); 3471 return ret; 3472} 3473 3474int repo_config_rename_section_in_file(struct repository *r, const char *config_filename, 3475 const char *old_name, const char *new_name) 3476{ 3477 return repo_config_copy_or_rename_section_in_file(r, config_filename, 3478 old_name, new_name, 0); 3479} 3480 3481int repo_config_rename_section(struct repository *r, const char *old_name, const char *new_name) 3482{ 3483 return repo_config_rename_section_in_file(r, NULL, old_name, new_name); 3484} 3485 3486int repo_config_copy_section_in_file(struct repository *r, const char *config_filename, 3487 const char *old_name, const char *new_name) 3488{ 3489 return repo_config_copy_or_rename_section_in_file(r, config_filename, 3490 old_name, new_name, 1); 3491} 3492 3493int repo_config_copy_section(struct repository *r, const char *old_name, const char *new_name) 3494{ 3495 return repo_config_copy_section_in_file(r, NULL, old_name, new_name); 3496} 3497 3498/* 3499 * Call this to report error for your variable that should not 3500 * get a boolean value (i.e. "[my] var" means "true"). 3501 */ 3502#undef config_error_nonbool 3503int config_error_nonbool(const char *var) 3504{ 3505 return error(_("missing value for '%s'"), var); 3506} 3507 3508int parse_config_key(const char *var, 3509 const char *section, 3510 const char **subsection, size_t *subsection_len, 3511 const char **key) 3512{ 3513 const char *dot; 3514 3515 /* Does it start with "section." ? */ 3516 if (!skip_prefix(var, section, &var) || *var != '.') 3517 return -1; 3518 3519 /* 3520 * Find the key; we don't know yet if we have a subsection, but we must 3521 * parse backwards from the end, since the subsection may have dots in 3522 * it, too. 3523 */ 3524 dot = strrchr(var, '.'); 3525 *key = dot + 1; 3526 3527 /* Did we have a subsection at all? */ 3528 if (dot == var) { 3529 if (subsection) { 3530 *subsection = NULL; 3531 *subsection_len = 0; 3532 } 3533 } 3534 else { 3535 if (!subsection) 3536 return -1; 3537 *subsection = var + 1; 3538 *subsection_len = dot - *subsection; 3539 } 3540 3541 return 0; 3542} 3543 3544const char *config_origin_type_name(enum config_origin_type type) 3545{ 3546 switch (type) { 3547 case CONFIG_ORIGIN_BLOB: 3548 return "blob"; 3549 case CONFIG_ORIGIN_FILE: 3550 return "file"; 3551 case CONFIG_ORIGIN_STDIN: 3552 return "standard input"; 3553 case CONFIG_ORIGIN_SUBMODULE_BLOB: 3554 return "submodule-blob"; 3555 case CONFIG_ORIGIN_CMDLINE: 3556 return "command line"; 3557 default: 3558 BUG("unknown config origin type"); 3559 } 3560} 3561 3562const char *config_scope_name(enum config_scope scope) 3563{ 3564 switch (scope) { 3565 case CONFIG_SCOPE_SYSTEM: 3566 return "system"; 3567 case CONFIG_SCOPE_GLOBAL: 3568 return "global"; 3569 case CONFIG_SCOPE_LOCAL: 3570 return "local"; 3571 case CONFIG_SCOPE_WORKTREE: 3572 return "worktree"; 3573 case CONFIG_SCOPE_COMMAND: 3574 return "command"; 3575 case CONFIG_SCOPE_SUBMODULE: 3576 return "submodule"; 3577 default: 3578 return "unknown"; 3579 } 3580} 3581 3582int lookup_config(const char **mapping, int nr_mapping, const char *var) 3583{ 3584 int i; 3585 3586 for (i = 0; i < nr_mapping; i++) { 3587 const char *name = mapping[i]; 3588 3589 if (name && !strcasecmp(var, name)) 3590 return i; 3591 } 3592 return -1; 3593}