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