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