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