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