1#include"cache.h" 2#include"dir.h" 3#include"string-list.h" 4 5static int inside_git_dir = -1; 6static int inside_work_tree = -1; 7 8/* 9 * Normalize "path", prepending the "prefix" for relative paths. If 10 * remaining_prefix is not NULL, return the actual prefix still 11 * remains in the path. For example, prefix = sub1/sub2/ and path is 12 * 13 * foo -> sub1/sub2/foo (full prefix) 14 * ../foo -> sub1/foo (remaining prefix is sub1/) 15 * ../../bar -> bar (no remaining prefix) 16 * ../../sub1/sub2/foo -> sub1/sub2/foo (but no remaining prefix) 17 * `pwd`/../bar -> sub1/bar (no remaining prefix) 18 */ 19char*prefix_path_gently(const char*prefix,int len, 20int*remaining_prefix,const char*path) 21{ 22const char*orig = path; 23char*sanitized; 24if(is_absolute_path(orig)) { 25const char*temp =real_path(path); 26 sanitized =xmalloc(len +strlen(temp) +1); 27strcpy(sanitized, temp); 28if(remaining_prefix) 29*remaining_prefix =0; 30}else{ 31 sanitized =xmalloc(len +strlen(path) +1); 32if(len) 33memcpy(sanitized, prefix, len); 34strcpy(sanitized + len, path); 35if(remaining_prefix) 36*remaining_prefix = len; 37} 38if(normalize_path_copy_len(sanitized, sanitized, remaining_prefix)) 39goto error_out; 40if(is_absolute_path(orig)) { 41size_t root_len, len, total; 42const char*work_tree =get_git_work_tree(); 43if(!work_tree) 44goto error_out; 45 len =strlen(work_tree); 46 root_len =offset_1st_component(work_tree); 47 total =strlen(sanitized) +1; 48if(strncmp(sanitized, work_tree, len) || 49(len > root_len && sanitized[len] !='\0'&& sanitized[len] !='/')) { 50 error_out: 51free(sanitized); 52return NULL; 53} 54if(sanitized[len] =='/') 55 len++; 56memmove(sanitized, sanitized + len, total - len); 57} 58return sanitized; 59} 60 61char*prefix_path(const char*prefix,int len,const char*path) 62{ 63char*r =prefix_path_gently(prefix, len, NULL, path); 64if(!r) 65die("'%s' is outside repository", path); 66return r; 67} 68 69intpath_inside_repo(const char*prefix,const char*path) 70{ 71int len = prefix ?strlen(prefix) :0; 72char*r =prefix_path_gently(prefix, len, NULL, path); 73if(r) { 74free(r); 75return1; 76} 77return0; 78} 79 80intcheck_filename(const char*prefix,const char*arg) 81{ 82const char*name; 83struct stat st; 84 85if(starts_with(arg,":/")) { 86if(arg[2] =='\0')/* ":/" is root dir, always exists */ 87return1; 88 name = arg +2; 89}else if(prefix) 90 name =prefix_filename(prefix,strlen(prefix), arg); 91else 92 name = arg; 93if(!lstat(name, &st)) 94return1;/* file exists */ 95if(errno == ENOENT || errno == ENOTDIR) 96return0;/* file does not exist */ 97die_errno("failed to stat '%s'", arg); 98} 99 100static void NORETURN die_verify_filename(const char*prefix, 101const char*arg, 102int diagnose_misspelt_rev) 103{ 104if(!diagnose_misspelt_rev) 105die("%s: no such path in the working tree.\n" 106"Use 'git <command> -- <path>...' to specify paths that do not exist locally.", 107 arg); 108/* 109 * Saying "'(icase)foo' does not exist in the index" when the 110 * user gave us ":(icase)foo" is just stupid. A magic pathspec 111 * begins with a colon and is followed by a non-alnum; do not 112 * let maybe_die_on_misspelt_object_name() even trigger. 113 */ 114if(!(arg[0] ==':'&& !isalnum(arg[1]))) 115maybe_die_on_misspelt_object_name(arg, prefix); 116 117/* ... or fall back the most general message. */ 118die("ambiguous argument '%s': unknown revision or path not in the working tree.\n" 119"Use '--' to separate paths from revisions, like this:\n" 120"'git <command> [<revision>...] -- [<file>...]'", arg); 121 122} 123 124/* 125 * Verify a filename that we got as an argument for a pathspec 126 * entry. Note that a filename that begins with "-" never verifies 127 * as true, because even if such a filename were to exist, we want 128 * it to be preceded by the "--" marker (or we want the user to 129 * use a format like "./-filename") 130 * 131 * The "diagnose_misspelt_rev" is used to provide a user-friendly 132 * diagnosis when dying upon finding that "name" is not a pathname. 133 * If set to 1, the diagnosis will try to diagnose "name" as an 134 * invalid object name (e.g. HEAD:foo). If set to 0, the diagnosis 135 * will only complain about an inexisting file. 136 * 137 * This function is typically called to check that a "file or rev" 138 * argument is unambiguous. In this case, the caller will want 139 * diagnose_misspelt_rev == 1 when verifying the first non-rev 140 * argument (which could have been a revision), and 141 * diagnose_misspelt_rev == 0 for the next ones (because we already 142 * saw a filename, there's not ambiguity anymore). 143 */ 144voidverify_filename(const char*prefix, 145const char*arg, 146int diagnose_misspelt_rev) 147{ 148if(*arg =='-') 149die("bad flag '%s' used after filename", arg); 150if(check_filename(prefix, arg)) 151return; 152die_verify_filename(prefix, arg, diagnose_misspelt_rev); 153} 154 155/* 156 * Opposite of the above: the command line did not have -- marker 157 * and we parsed the arg as a refname. It should not be interpretable 158 * as a filename. 159 */ 160voidverify_non_filename(const char*prefix,const char*arg) 161{ 162if(!is_inside_work_tree() ||is_inside_git_dir()) 163return; 164if(*arg =='-') 165return;/* flag */ 166if(!check_filename(prefix, arg)) 167return; 168die("ambiguous argument '%s': both revision and filename\n" 169"Use '--' to separate paths from revisions, like this:\n" 170"'git <command> [<revision>...] -- [<file>...]'", arg); 171} 172 173 174/* 175 * Test if it looks like we're at a git directory. 176 * We want to see: 177 * 178 * - either an objects/ directory _or_ the proper 179 * GIT_OBJECT_DIRECTORY environment variable 180 * - a refs/ directory 181 * - either a HEAD symlink or a HEAD file that is formatted as 182 * a proper "ref:", or a regular file HEAD that has a properly 183 * formatted sha1 object name. 184 */ 185intis_git_directory(const char*suspect) 186{ 187char path[PATH_MAX]; 188size_t len =strlen(suspect); 189 190if(PATH_MAX <= len +strlen("/objects")) 191die("Too long path: %.*s",60, suspect); 192strcpy(path, suspect); 193if(getenv(DB_ENVIRONMENT)) { 194if(access(getenv(DB_ENVIRONMENT), X_OK)) 195return0; 196} 197else{ 198strcpy(path + len,"/objects"); 199if(access(path, X_OK)) 200return0; 201} 202 203strcpy(path + len,"/refs"); 204if(access(path, X_OK)) 205return0; 206 207strcpy(path + len,"/HEAD"); 208if(validate_headref(path)) 209return0; 210 211return1; 212} 213 214intis_inside_git_dir(void) 215{ 216if(inside_git_dir <0) 217 inside_git_dir =is_inside_dir(get_git_dir()); 218return inside_git_dir; 219} 220 221intis_inside_work_tree(void) 222{ 223if(inside_work_tree <0) 224 inside_work_tree =is_inside_dir(get_git_work_tree()); 225return inside_work_tree; 226} 227 228voidsetup_work_tree(void) 229{ 230const char*work_tree, *git_dir; 231static int initialized =0; 232 233if(initialized) 234return; 235 work_tree =get_git_work_tree(); 236 git_dir =get_git_dir(); 237if(!is_absolute_path(git_dir)) 238 git_dir =real_path(get_git_dir()); 239if(!work_tree ||chdir(work_tree)) 240die("This operation must be run in a work tree"); 241 242/* 243 * Make sure subsequent git processes find correct worktree 244 * if $GIT_WORK_TREE is set relative 245 */ 246if(getenv(GIT_WORK_TREE_ENVIRONMENT)) 247setenv(GIT_WORK_TREE_ENVIRONMENT,".",1); 248 249set_git_dir(remove_leading_path(git_dir, work_tree)); 250 initialized =1; 251} 252 253static intcheck_repository_format_gently(const char*gitdir,int*nongit_ok) 254{ 255char repo_config[PATH_MAX+1]; 256 257/* 258 * git_config() can't be used here because it calls git_pathdup() 259 * to get $GIT_CONFIG/config. That call will make setup_git_env() 260 * set git_dir to ".git". 261 * 262 * We are in gitdir setup, no git dir has been found useable yet. 263 * Use a gentler version of git_config() to check if this repo 264 * is a good one. 265 */ 266snprintf(repo_config, PATH_MAX,"%s/config", gitdir); 267git_config_early(check_repository_format_version, NULL, repo_config); 268if(GIT_REPO_VERSION < repository_format_version) { 269if(!nongit_ok) 270die("Expected git repo version <=%d, found%d", 271 GIT_REPO_VERSION, repository_format_version); 272warning("Expected git repo version <=%d, found%d", 273 GIT_REPO_VERSION, repository_format_version); 274warning("Please upgrade Git"); 275*nongit_ok = -1; 276return-1; 277} 278return0; 279} 280 281/* 282 * Try to read the location of the git directory from the .git file, 283 * return path to git directory if found. 284 */ 285const char*read_gitfile(const char*path) 286{ 287char*buf; 288char*dir; 289const char*slash; 290struct stat st; 291int fd; 292 ssize_t len; 293 294if(stat(path, &st)) 295return NULL; 296if(!S_ISREG(st.st_mode)) 297return NULL; 298 fd =open(path, O_RDONLY); 299if(fd <0) 300die_errno("Error opening '%s'", path); 301 buf =xmalloc(st.st_size +1); 302 len =read_in_full(fd, buf, st.st_size); 303close(fd); 304if(len != st.st_size) 305die("Error reading%s", path); 306 buf[len] ='\0'; 307if(!starts_with(buf,"gitdir: ")) 308die("Invalid gitfile format:%s", path); 309while(buf[len -1] =='\n'|| buf[len -1] =='\r') 310 len--; 311if(len <9) 312die("No path in gitfile:%s", path); 313 buf[len] ='\0'; 314 dir = buf +8; 315 316if(!is_absolute_path(dir) && (slash =strrchr(path,'/'))) { 317size_t pathlen = slash+1- path; 318size_t dirlen = pathlen + len -8; 319 dir =xmalloc(dirlen +1); 320strncpy(dir, path, pathlen); 321strncpy(dir + pathlen, buf +8, len -8); 322 dir[dirlen] ='\0'; 323free(buf); 324 buf = dir; 325} 326 327if(!is_git_directory(dir)) 328die("Not a git repository:%s", dir); 329 path =real_path(dir); 330 331free(buf); 332return path; 333} 334 335static const char*setup_explicit_git_dir(const char*gitdirenv, 336char*cwd,int len, 337int*nongit_ok) 338{ 339const char*work_tree_env =getenv(GIT_WORK_TREE_ENVIRONMENT); 340const char*worktree; 341char*gitfile; 342int offset; 343 344if(PATH_MAX -40<strlen(gitdirenv)) 345die("'$%s' too big", GIT_DIR_ENVIRONMENT); 346 347 gitfile = (char*)read_gitfile(gitdirenv); 348if(gitfile) { 349 gitfile =xstrdup(gitfile); 350 gitdirenv = gitfile; 351} 352 353if(!is_git_directory(gitdirenv)) { 354if(nongit_ok) { 355*nongit_ok =1; 356free(gitfile); 357return NULL; 358} 359die("Not a git repository: '%s'", gitdirenv); 360} 361 362if(check_repository_format_gently(gitdirenv, nongit_ok)) { 363free(gitfile); 364return NULL; 365} 366 367/* #3, #7, #11, #15, #19, #23, #27, #31 (see t1510) */ 368if(work_tree_env) 369set_git_work_tree(work_tree_env); 370else if(is_bare_repository_cfg >0) { 371if(git_work_tree_cfg)/* #22.2, #30 */ 372die("core.bare and core.worktree do not make sense"); 373 374/* #18, #26 */ 375set_git_dir(gitdirenv); 376free(gitfile); 377return NULL; 378} 379else if(git_work_tree_cfg) {/* #6, #14 */ 380if(is_absolute_path(git_work_tree_cfg)) 381set_git_work_tree(git_work_tree_cfg); 382else{ 383char core_worktree[PATH_MAX]; 384if(chdir(gitdirenv)) 385die_errno("Could not chdir to '%s'", gitdirenv); 386if(chdir(git_work_tree_cfg)) 387die_errno("Could not chdir to '%s'", git_work_tree_cfg); 388if(!getcwd(core_worktree, PATH_MAX)) 389die_errno("Could not get directory '%s'", git_work_tree_cfg); 390if(chdir(cwd)) 391die_errno("Could not come back to cwd"); 392set_git_work_tree(core_worktree); 393} 394} 395else if(!git_env_bool(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT,1)) { 396/* #16d */ 397set_git_dir(gitdirenv); 398free(gitfile); 399return NULL; 400} 401else/* #2, #10 */ 402set_git_work_tree("."); 403 404/* set_git_work_tree() must have been called by now */ 405 worktree =get_git_work_tree(); 406 407/* both get_git_work_tree() and cwd are already normalized */ 408if(!strcmp(cwd, worktree)) {/* cwd == worktree */ 409set_git_dir(gitdirenv); 410free(gitfile); 411return NULL; 412} 413 414 offset =dir_inside_of(cwd, worktree); 415if(offset >=0) {/* cwd inside worktree? */ 416set_git_dir(real_path(gitdirenv)); 417if(chdir(worktree)) 418die_errno("Could not chdir to '%s'", worktree); 419 cwd[len++] ='/'; 420 cwd[len] ='\0'; 421free(gitfile); 422return cwd + offset; 423} 424 425/* cwd outside worktree */ 426set_git_dir(gitdirenv); 427free(gitfile); 428return NULL; 429} 430 431static const char*setup_discovered_git_dir(const char*gitdir, 432char*cwd,int offset,int len, 433int*nongit_ok) 434{ 435if(check_repository_format_gently(gitdir, nongit_ok)) 436return NULL; 437 438/* --work-tree is set without --git-dir; use discovered one */ 439if(getenv(GIT_WORK_TREE_ENVIRONMENT) || git_work_tree_cfg) { 440if(offset != len && !is_absolute_path(gitdir)) 441 gitdir =xstrdup(real_path(gitdir)); 442if(chdir(cwd)) 443die_errno("Could not come back to cwd"); 444returnsetup_explicit_git_dir(gitdir, cwd, len, nongit_ok); 445} 446 447/* #16.2, #17.2, #20.2, #21.2, #24, #25, #28, #29 (see t1510) */ 448if(is_bare_repository_cfg >0) { 449set_git_dir(offset == len ? gitdir :real_path(gitdir)); 450if(chdir(cwd)) 451die_errno("Could not come back to cwd"); 452return NULL; 453} 454 455/* #0, #1, #5, #8, #9, #12, #13 */ 456set_git_work_tree("."); 457if(strcmp(gitdir, DEFAULT_GIT_DIR_ENVIRONMENT)) 458set_git_dir(gitdir); 459 inside_git_dir =0; 460 inside_work_tree =1; 461if(offset == len) 462return NULL; 463 464/* Make "offset" point to past the '/', and add a '/' at the end */ 465 offset++; 466 cwd[len++] ='/'; 467 cwd[len] =0; 468return cwd + offset; 469} 470 471/* #16.1, #17.1, #20.1, #21.1, #22.1 (see t1510) */ 472static const char*setup_bare_git_dir(char*cwd,int offset,int len,int*nongit_ok) 473{ 474int root_len; 475 476if(check_repository_format_gently(".", nongit_ok)) 477return NULL; 478 479setenv(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT,"0",1); 480 481/* --work-tree is set without --git-dir; use discovered one */ 482if(getenv(GIT_WORK_TREE_ENVIRONMENT) || git_work_tree_cfg) { 483const char*gitdir; 484 485 gitdir = offset == len ?".":xmemdupz(cwd, offset); 486if(chdir(cwd)) 487die_errno("Could not come back to cwd"); 488returnsetup_explicit_git_dir(gitdir, cwd, len, nongit_ok); 489} 490 491 inside_git_dir =1; 492 inside_work_tree =0; 493if(offset != len) { 494if(chdir(cwd)) 495die_errno("Cannot come back to cwd"); 496 root_len =offset_1st_component(cwd); 497 cwd[offset > root_len ? offset : root_len] ='\0'; 498set_git_dir(cwd); 499} 500else 501set_git_dir("."); 502return NULL; 503} 504 505static const char*setup_nongit(const char*cwd,int*nongit_ok) 506{ 507if(!nongit_ok) 508die("Not a git repository (or any of the parent directories):%s", DEFAULT_GIT_DIR_ENVIRONMENT); 509if(chdir(cwd)) 510die_errno("Cannot come back to cwd"); 511*nongit_ok =1; 512return NULL; 513} 514 515static dev_t get_device_or_die(const char*path,const char*prefix,int prefix_len) 516{ 517struct stat buf; 518if(stat(path, &buf)) { 519die_errno("failed to stat '%*s%s%s'", 520 prefix_len, 521 prefix ? prefix :"", 522 prefix ?"/":"", path); 523} 524return buf.st_dev; 525} 526 527/* 528 * A "string_list_each_func_t" function that canonicalizes an entry 529 * from GIT_CEILING_DIRECTORIES using real_path_if_valid(), or 530 * discards it if unusable. The presence of an empty entry in 531 * GIT_CEILING_DIRECTORIES turns off canonicalization for all 532 * subsequent entries. 533 */ 534static intcanonicalize_ceiling_entry(struct string_list_item *item, 535void*cb_data) 536{ 537int*empty_entry_found = cb_data; 538char*ceil = item->string; 539 540if(!*ceil) { 541*empty_entry_found =1; 542return0; 543}else if(!is_absolute_path(ceil)) { 544return0; 545}else if(*empty_entry_found) { 546/* Keep entry but do not canonicalize it */ 547return1; 548}else{ 549const char*real_path =real_path_if_valid(ceil); 550if(!real_path) 551return0; 552free(item->string); 553 item->string =xstrdup(real_path); 554return1; 555} 556} 557 558/* 559 * We cannot decide in this function whether we are in the work tree or 560 * not, since the config can only be read _after_ this function was called. 561 */ 562static const char*setup_git_directory_gently_1(int*nongit_ok) 563{ 564const char*env_ceiling_dirs =getenv(CEILING_DIRECTORIES_ENVIRONMENT); 565struct string_list ceiling_dirs = STRING_LIST_INIT_DUP; 566static char cwd[PATH_MAX +1]; 567const char*gitdirenv, *ret; 568char*gitfile; 569int len, offset, offset_parent, ceil_offset = -1; 570 dev_t current_device =0; 571int one_filesystem =1; 572 573/* 574 * Let's assume that we are in a git repository. 575 * If it turns out later that we are somewhere else, the value will be 576 * updated accordingly. 577 */ 578if(nongit_ok) 579*nongit_ok =0; 580 581if(!getcwd(cwd,sizeof(cwd) -1)) 582die_errno("Unable to read current working directory"); 583 offset = len =strlen(cwd); 584 585/* 586 * If GIT_DIR is set explicitly, we're not going 587 * to do any discovery, but we still do repository 588 * validation. 589 */ 590 gitdirenv =getenv(GIT_DIR_ENVIRONMENT); 591if(gitdirenv) 592returnsetup_explicit_git_dir(gitdirenv, cwd, len, nongit_ok); 593 594if(env_ceiling_dirs) { 595int empty_entry_found =0; 596 597string_list_split(&ceiling_dirs, env_ceiling_dirs, PATH_SEP, -1); 598filter_string_list(&ceiling_dirs,0, 599 canonicalize_ceiling_entry, &empty_entry_found); 600 ceil_offset =longest_ancestor_length(cwd, &ceiling_dirs); 601string_list_clear(&ceiling_dirs,0); 602} 603 604if(ceil_offset <0&&has_dos_drive_prefix(cwd)) 605 ceil_offset =1; 606 607/* 608 * Test in the following order (relative to the cwd): 609 * - .git (file containing "gitdir: <path>") 610 * - .git/ 611 * - ./ (bare) 612 * - ../.git 613 * - ../.git/ 614 * - ../ (bare) 615 * - ../../.git/ 616 * etc. 617 */ 618 one_filesystem = !git_env_bool("GIT_DISCOVERY_ACROSS_FILESYSTEM",0); 619if(one_filesystem) 620 current_device =get_device_or_die(".", NULL,0); 621for(;;) { 622 gitfile = (char*)read_gitfile(DEFAULT_GIT_DIR_ENVIRONMENT); 623if(gitfile) 624 gitdirenv = gitfile =xstrdup(gitfile); 625else{ 626if(is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT)) 627 gitdirenv = DEFAULT_GIT_DIR_ENVIRONMENT; 628} 629 630if(gitdirenv) { 631 ret =setup_discovered_git_dir(gitdirenv, 632 cwd, offset, len, 633 nongit_ok); 634free(gitfile); 635return ret; 636} 637free(gitfile); 638 639if(is_git_directory(".")) 640returnsetup_bare_git_dir(cwd, offset, len, nongit_ok); 641 642 offset_parent = offset; 643while(--offset_parent > ceil_offset && cwd[offset_parent] !='/'); 644if(offset_parent <= ceil_offset) 645returnsetup_nongit(cwd, nongit_ok); 646if(one_filesystem) { 647 dev_t parent_device =get_device_or_die("..", cwd, offset); 648if(parent_device != current_device) { 649if(nongit_ok) { 650if(chdir(cwd)) 651die_errno("Cannot come back to cwd"); 652*nongit_ok =1; 653return NULL; 654} 655 cwd[offset] ='\0'; 656die("Not a git repository (or any parent up to mount point%s)\n" 657"Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).", cwd); 658} 659} 660if(chdir("..")) { 661 cwd[offset] ='\0'; 662die_errno("Cannot change to '%s/..'", cwd); 663} 664 offset = offset_parent; 665} 666} 667 668const char*setup_git_directory_gently(int*nongit_ok) 669{ 670const char*prefix; 671 672 prefix =setup_git_directory_gently_1(nongit_ok); 673if(prefix) 674setenv(GIT_PREFIX_ENVIRONMENT, prefix,1); 675else 676setenv(GIT_PREFIX_ENVIRONMENT,"",1); 677 678if(startup_info) { 679 startup_info->have_repository = !nongit_ok || !*nongit_ok; 680 startup_info->prefix = prefix; 681} 682return prefix; 683} 684 685intgit_config_perm(const char*var,const char*value) 686{ 687int i; 688char*endptr; 689 690if(value == NULL) 691return PERM_GROUP; 692 693if(!strcmp(value,"umask")) 694return PERM_UMASK; 695if(!strcmp(value,"group")) 696return PERM_GROUP; 697if(!strcmp(value,"all") || 698!strcmp(value,"world") || 699!strcmp(value,"everybody")) 700return PERM_EVERYBODY; 701 702/* Parse octal numbers */ 703 i =strtol(value, &endptr,8); 704 705/* If not an octal number, maybe true/false? */ 706if(*endptr !=0) 707returngit_config_bool(var, value) ? PERM_GROUP : PERM_UMASK; 708 709/* 710 * Treat values 0, 1 and 2 as compatibility cases, otherwise it is 711 * a chmod value to restrict to. 712 */ 713switch(i) { 714case PERM_UMASK:/* 0 */ 715return PERM_UMASK; 716case OLD_PERM_GROUP:/* 1 */ 717return PERM_GROUP; 718case OLD_PERM_EVERYBODY:/* 2 */ 719return PERM_EVERYBODY; 720} 721 722/* A filemode value was given: 0xxx */ 723 724if((i &0600) !=0600) 725die("Problem with core.sharedRepository filemode value " 726"(0%.3o).\nThe owner of files must always have " 727"read and write permissions.", i); 728 729/* 730 * Mask filemode value. Others can not get write permission. 731 * x flags for directories are handled separately. 732 */ 733return-(i &0666); 734} 735 736intcheck_repository_format_version(const char*var,const char*value,void*cb) 737{ 738if(strcmp(var,"core.repositoryformatversion") ==0) 739 repository_format_version =git_config_int(var, value); 740else if(strcmp(var,"core.sharedrepository") ==0) 741 shared_repository =git_config_perm(var, value); 742else if(strcmp(var,"core.bare") ==0) { 743 is_bare_repository_cfg =git_config_bool(var, value); 744if(is_bare_repository_cfg ==1) 745 inside_work_tree = -1; 746}else if(strcmp(var,"core.worktree") ==0) { 747if(!value) 748returnconfig_error_nonbool(var); 749free(git_work_tree_cfg); 750 git_work_tree_cfg =xstrdup(value); 751 inside_work_tree = -1; 752} 753return0; 754} 755 756intcheck_repository_format(void) 757{ 758returncheck_repository_format_gently(get_git_dir(), NULL); 759} 760 761/* 762 * Returns the "prefix", a path to the current working directory 763 * relative to the work tree root, or NULL, if the current working 764 * directory is not a strict subdirectory of the work tree root. The 765 * prefix always ends with a '/' character. 766 */ 767const char*setup_git_directory(void) 768{ 769returnsetup_git_directory_gently(NULL); 770} 771 772const char*resolve_gitdir(const char*suspect) 773{ 774if(is_git_directory(suspect)) 775return suspect; 776returnread_gitfile(suspect); 777} 778 779/* if any standard file descriptor is missing open it to /dev/null */ 780voidsanitize_stdfds(void) 781{ 782int fd =open("/dev/null", O_RDWR,0); 783while(fd != -1&& fd <2) 784 fd =dup(fd); 785if(fd == -1) 786die_errno("open /dev/null or dup failed"); 787if(fd >2) 788close(fd); 789}