setup.con commit git log -p -m: document -m and honor --first-parent (88d9d45)
   1#include "cache.h"
   2#include "dir.h"
   3
   4static int inside_git_dir = -1;
   5static int inside_work_tree = -1;
   6
   7const char *prefix_path(const char *prefix, int len, const char *path)
   8{
   9        const char *orig = path;
  10        char *sanitized = xmalloc(len + strlen(path) + 1);
  11        if (is_absolute_path(orig))
  12                strcpy(sanitized, path);
  13        else {
  14                if (len)
  15                        memcpy(sanitized, prefix, len);
  16                strcpy(sanitized + len, path);
  17        }
  18        if (normalize_path_copy(sanitized, sanitized))
  19                goto error_out;
  20        if (is_absolute_path(orig)) {
  21                size_t len, total;
  22                const char *work_tree = get_git_work_tree();
  23                if (!work_tree)
  24                        goto error_out;
  25                len = strlen(work_tree);
  26                total = strlen(sanitized) + 1;
  27                if (strncmp(sanitized, work_tree, len) ||
  28                    (sanitized[len] != '\0' && sanitized[len] != '/')) {
  29                error_out:
  30                        die("'%s' is outside repository", orig);
  31                }
  32                if (sanitized[len] == '/')
  33                        len++;
  34                memmove(sanitized, sanitized + len, total - len);
  35        }
  36        return sanitized;
  37}
  38
  39/*
  40 * Unlike prefix_path, this should be used if the named file does
  41 * not have to interact with index entry; i.e. name of a random file
  42 * on the filesystem.
  43 */
  44const char *prefix_filename(const char *pfx, int pfx_len, const char *arg)
  45{
  46        static char path[PATH_MAX];
  47#ifndef WIN32
  48        if (!pfx || !*pfx || is_absolute_path(arg))
  49                return arg;
  50        memcpy(path, pfx, pfx_len);
  51        strcpy(path + pfx_len, arg);
  52#else
  53        char *p;
  54        /* don't add prefix to absolute paths, but still replace '\' by '/' */
  55        if (is_absolute_path(arg))
  56                pfx_len = 0;
  57        else
  58                memcpy(path, pfx, pfx_len);
  59        strcpy(path + pfx_len, arg);
  60        for (p = path + pfx_len; *p; p++)
  61                if (*p == '\\')
  62                        *p = '/';
  63#endif
  64        return path;
  65}
  66
  67int check_filename(const char *prefix, const char *arg)
  68{
  69        const char *name;
  70        struct stat st;
  71
  72        name = prefix ? prefix_filename(prefix, strlen(prefix), arg) : arg;
  73        if (!lstat(name, &st))
  74                return 1; /* file exists */
  75        if (errno == ENOENT || errno == ENOTDIR)
  76                return 0; /* file does not exist */
  77        die_errno("failed to stat '%s'", arg);
  78}
  79
  80static void NORETURN die_verify_filename(const char *prefix, const char *arg)
  81{
  82        unsigned char sha1[20];
  83        unsigned mode;
  84        /* try a detailed diagnostic ... */
  85        get_sha1_with_mode_1(arg, sha1, &mode, 0, prefix);
  86        /* ... or fall back the most general message. */
  87        die("ambiguous argument '%s': unknown revision or path not in the working tree.\n"
  88            "Use '--' to separate paths from revisions", arg);
  89
  90}
  91
  92/*
  93 * Verify a filename that we got as an argument for a pathspec
  94 * entry. Note that a filename that begins with "-" never verifies
  95 * as true, because even if such a filename were to exist, we want
  96 * it to be preceded by the "--" marker (or we want the user to
  97 * use a format like "./-filename")
  98 */
  99void verify_filename(const char *prefix, const char *arg)
 100{
 101        if (*arg == '-')
 102                die("bad flag '%s' used after filename", arg);
 103        if (check_filename(prefix, arg))
 104                return;
 105        die_verify_filename(prefix, arg);
 106}
 107
 108/*
 109 * Opposite of the above: the command line did not have -- marker
 110 * and we parsed the arg as a refname.  It should not be interpretable
 111 * as a filename.
 112 */
 113void verify_non_filename(const char *prefix, const char *arg)
 114{
 115        if (!is_inside_work_tree() || is_inside_git_dir())
 116                return;
 117        if (*arg == '-')
 118                return; /* flag */
 119        if (!check_filename(prefix, arg))
 120                return;
 121        die("ambiguous argument '%s': both revision and filename\n"
 122            "Use '--' to separate filenames from revisions", arg);
 123}
 124
 125const char **get_pathspec(const char *prefix, const char **pathspec)
 126{
 127        const char *entry = *pathspec;
 128        const char **src, **dst;
 129        int prefixlen;
 130
 131        if (!prefix && !entry)
 132                return NULL;
 133
 134        if (!entry) {
 135                static const char *spec[2];
 136                spec[0] = prefix;
 137                spec[1] = NULL;
 138                return spec;
 139        }
 140
 141        /* Otherwise we have to re-write the entries.. */
 142        src = pathspec;
 143        dst = pathspec;
 144        prefixlen = prefix ? strlen(prefix) : 0;
 145        while (*src) {
 146                const char *p = prefix_path(prefix, prefixlen, *src);
 147                *(dst++) = p;
 148                src++;
 149        }
 150        *dst = NULL;
 151        if (!*pathspec)
 152                return NULL;
 153        return pathspec;
 154}
 155
 156/*
 157 * Test if it looks like we're at a git directory.
 158 * We want to see:
 159 *
 160 *  - either an objects/ directory _or_ the proper
 161 *    GIT_OBJECT_DIRECTORY environment variable
 162 *  - a refs/ directory
 163 *  - either a HEAD symlink or a HEAD file that is formatted as
 164 *    a proper "ref:", or a regular file HEAD that has a properly
 165 *    formatted sha1 object name.
 166 */
 167static int is_git_directory(const char *suspect)
 168{
 169        char path[PATH_MAX];
 170        size_t len = strlen(suspect);
 171
 172        strcpy(path, suspect);
 173        if (getenv(DB_ENVIRONMENT)) {
 174                if (access(getenv(DB_ENVIRONMENT), X_OK))
 175                        return 0;
 176        }
 177        else {
 178                strcpy(path + len, "/objects");
 179                if (access(path, X_OK))
 180                        return 0;
 181        }
 182
 183        strcpy(path + len, "/refs");
 184        if (access(path, X_OK))
 185                return 0;
 186
 187        strcpy(path + len, "/HEAD");
 188        if (validate_headref(path))
 189                return 0;
 190
 191        return 1;
 192}
 193
 194int is_inside_git_dir(void)
 195{
 196        if (inside_git_dir < 0)
 197                inside_git_dir = is_inside_dir(get_git_dir());
 198        return inside_git_dir;
 199}
 200
 201int is_inside_work_tree(void)
 202{
 203        if (inside_work_tree < 0)
 204                inside_work_tree = is_inside_dir(get_git_work_tree());
 205        return inside_work_tree;
 206}
 207
 208/*
 209 * set_work_tree() is only ever called if you set GIT_DIR explicitely.
 210 * The old behaviour (which we retain here) is to set the work tree root
 211 * to the cwd, unless overridden by the config, the command line, or
 212 * GIT_WORK_TREE.
 213 */
 214static const char *set_work_tree(const char *dir)
 215{
 216        char buffer[PATH_MAX + 1];
 217
 218        if (!getcwd(buffer, sizeof(buffer)))
 219                die ("Could not get the current working directory");
 220        git_work_tree_cfg = xstrdup(buffer);
 221        inside_work_tree = 1;
 222
 223        return NULL;
 224}
 225
 226void setup_work_tree(void)
 227{
 228        const char *work_tree, *git_dir;
 229        static int initialized = 0;
 230
 231        if (initialized)
 232                return;
 233        work_tree = get_git_work_tree();
 234        git_dir = get_git_dir();
 235        if (!is_absolute_path(git_dir))
 236                git_dir = make_absolute_path(git_dir);
 237        if (!work_tree || chdir(work_tree))
 238                die("This operation must be run in a work tree");
 239        set_git_dir(make_relative_path(git_dir, work_tree));
 240        initialized = 1;
 241}
 242
 243static int check_repository_format_gently(int *nongit_ok)
 244{
 245        git_config(check_repository_format_version, NULL);
 246        if (GIT_REPO_VERSION < repository_format_version) {
 247                if (!nongit_ok)
 248                        die ("Expected git repo version <= %d, found %d",
 249                             GIT_REPO_VERSION, repository_format_version);
 250                warning("Expected git repo version <= %d, found %d",
 251                        GIT_REPO_VERSION, repository_format_version);
 252                warning("Please upgrade Git");
 253                *nongit_ok = -1;
 254                return -1;
 255        }
 256        return 0;
 257}
 258
 259/*
 260 * Try to read the location of the git directory from the .git file,
 261 * return path to git directory if found.
 262 */
 263const char *read_gitfile_gently(const char *path)
 264{
 265        char *buf;
 266        char *dir;
 267        const char *slash;
 268        struct stat st;
 269        int fd;
 270        size_t len;
 271
 272        if (stat(path, &st))
 273                return NULL;
 274        if (!S_ISREG(st.st_mode))
 275                return NULL;
 276        fd = open(path, O_RDONLY);
 277        if (fd < 0)
 278                die_errno("Error opening '%s'", path);
 279        buf = xmalloc(st.st_size + 1);
 280        len = read_in_full(fd, buf, st.st_size);
 281        close(fd);
 282        if (len != st.st_size)
 283                die("Error reading %s", path);
 284        buf[len] = '\0';
 285        if (prefixcmp(buf, "gitdir: "))
 286                die("Invalid gitfile format: %s", path);
 287        while (buf[len - 1] == '\n' || buf[len - 1] == '\r')
 288                len--;
 289        if (len < 9)
 290                die("No path in gitfile: %s", path);
 291        buf[len] = '\0';
 292        dir = buf + 8;
 293
 294        if (!is_absolute_path(dir) && (slash = strrchr(path, '/'))) {
 295                size_t pathlen = slash+1 - path;
 296                size_t dirlen = pathlen + len - 8;
 297                dir = xmalloc(dirlen + 1);
 298                strncpy(dir, path, pathlen);
 299                strncpy(dir + pathlen, buf + 8, len - 8);
 300                dir[dirlen] = '\0';
 301                free(buf);
 302                buf = dir;
 303        }
 304
 305        if (!is_git_directory(dir))
 306                die("Not a git repository: %s", dir);
 307        path = make_absolute_path(dir);
 308
 309        free(buf);
 310        return path;
 311}
 312
 313/*
 314 * We cannot decide in this function whether we are in the work tree or
 315 * not, since the config can only be read _after_ this function was called.
 316 */
 317const char *setup_git_directory_gently(int *nongit_ok)
 318{
 319        const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT);
 320        const char *env_ceiling_dirs = getenv(CEILING_DIRECTORIES_ENVIRONMENT);
 321        static char cwd[PATH_MAX+1];
 322        const char *gitdirenv;
 323        const char *gitfile_dir;
 324        int len, offset, ceil_offset;
 325
 326        /*
 327         * Let's assume that we are in a git repository.
 328         * If it turns out later that we are somewhere else, the value will be
 329         * updated accordingly.
 330         */
 331        if (nongit_ok)
 332                *nongit_ok = 0;
 333
 334        /*
 335         * If GIT_DIR is set explicitly, we're not going
 336         * to do any discovery, but we still do repository
 337         * validation.
 338         */
 339        gitdirenv = getenv(GIT_DIR_ENVIRONMENT);
 340        if (gitdirenv) {
 341                if (PATH_MAX - 40 < strlen(gitdirenv))
 342                        die("'$%s' too big", GIT_DIR_ENVIRONMENT);
 343                if (is_git_directory(gitdirenv)) {
 344                        static char buffer[1024 + 1];
 345                        const char *retval;
 346
 347                        if (!work_tree_env) {
 348                                retval = set_work_tree(gitdirenv);
 349                                /* config may override worktree */
 350                                if (check_repository_format_gently(nongit_ok))
 351                                        return NULL;
 352                                return retval;
 353                        }
 354                        if (check_repository_format_gently(nongit_ok))
 355                                return NULL;
 356                        retval = get_relative_cwd(buffer, sizeof(buffer) - 1,
 357                                        get_git_work_tree());
 358                        if (!retval || !*retval)
 359                                return NULL;
 360                        set_git_dir(make_absolute_path(gitdirenv));
 361                        if (chdir(work_tree_env) < 0)
 362                                die_errno ("Could not chdir to '%s'", work_tree_env);
 363                        strcat(buffer, "/");
 364                        return retval;
 365                }
 366                if (nongit_ok) {
 367                        *nongit_ok = 1;
 368                        return NULL;
 369                }
 370                die("Not a git repository: '%s'", gitdirenv);
 371        }
 372
 373        if (!getcwd(cwd, sizeof(cwd)-1))
 374                die_errno("Unable to read current working directory");
 375
 376        ceil_offset = longest_ancestor_length(cwd, env_ceiling_dirs);
 377        if (ceil_offset < 0 && has_dos_drive_prefix(cwd))
 378                ceil_offset = 1;
 379
 380        /*
 381         * Test in the following order (relative to the cwd):
 382         * - .git (file containing "gitdir: <path>")
 383         * - .git/
 384         * - ./ (bare)
 385         * - ../.git
 386         * - ../.git/
 387         * - ../ (bare)
 388         * - ../../.git/
 389         *   etc.
 390         */
 391        offset = len = strlen(cwd);
 392        for (;;) {
 393                gitfile_dir = read_gitfile_gently(DEFAULT_GIT_DIR_ENVIRONMENT);
 394                if (gitfile_dir) {
 395                        if (set_git_dir(gitfile_dir))
 396                                die("Repository setup failed");
 397                        break;
 398                }
 399                if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT))
 400                        break;
 401                if (is_git_directory(".")) {
 402                        inside_git_dir = 1;
 403                        if (!work_tree_env)
 404                                inside_work_tree = 0;
 405                        if (offset != len) {
 406                                cwd[offset] = '\0';
 407                                setenv(GIT_DIR_ENVIRONMENT, cwd, 1);
 408                        } else
 409                                setenv(GIT_DIR_ENVIRONMENT, ".", 1);
 410                        check_repository_format_gently(nongit_ok);
 411                        return NULL;
 412                }
 413                while (--offset > ceil_offset && cwd[offset] != '/');
 414                if (offset <= ceil_offset) {
 415                        if (nongit_ok) {
 416                                if (chdir(cwd))
 417                                        die_errno("Cannot come back to cwd");
 418                                *nongit_ok = 1;
 419                                return NULL;
 420                        }
 421                        die("Not a git repository (or any of the parent directories): %s", DEFAULT_GIT_DIR_ENVIRONMENT);
 422                }
 423                if (chdir(".."))
 424                        die_errno("Cannot change to '%s/..'", cwd);
 425        }
 426
 427        inside_git_dir = 0;
 428        if (!work_tree_env)
 429                inside_work_tree = 1;
 430        git_work_tree_cfg = xstrndup(cwd, offset);
 431        if (check_repository_format_gently(nongit_ok))
 432                return NULL;
 433        if (offset == len)
 434                return NULL;
 435
 436        /* Make "offset" point to past the '/', and add a '/' at the end */
 437        offset++;
 438        cwd[len++] = '/';
 439        cwd[len] = 0;
 440        return cwd + offset;
 441}
 442
 443int git_config_perm(const char *var, const char *value)
 444{
 445        int i;
 446        char *endptr;
 447
 448        if (value == NULL)
 449                return PERM_GROUP;
 450
 451        if (!strcmp(value, "umask"))
 452                return PERM_UMASK;
 453        if (!strcmp(value, "group"))
 454                return PERM_GROUP;
 455        if (!strcmp(value, "all") ||
 456            !strcmp(value, "world") ||
 457            !strcmp(value, "everybody"))
 458                return PERM_EVERYBODY;
 459
 460        /* Parse octal numbers */
 461        i = strtol(value, &endptr, 8);
 462
 463        /* If not an octal number, maybe true/false? */
 464        if (*endptr != 0)
 465                return git_config_bool(var, value) ? PERM_GROUP : PERM_UMASK;
 466
 467        /*
 468         * Treat values 0, 1 and 2 as compatibility cases, otherwise it is
 469         * a chmod value to restrict to.
 470         */
 471        switch (i) {
 472        case PERM_UMASK:               /* 0 */
 473                return PERM_UMASK;
 474        case OLD_PERM_GROUP:           /* 1 */
 475                return PERM_GROUP;
 476        case OLD_PERM_EVERYBODY:       /* 2 */
 477                return PERM_EVERYBODY;
 478        }
 479
 480        /* A filemode value was given: 0xxx */
 481
 482        if ((i & 0600) != 0600)
 483                die("Problem with core.sharedRepository filemode value "
 484                    "(0%.3o).\nThe owner of files must always have "
 485                    "read and write permissions.", i);
 486
 487        /*
 488         * Mask filemode value. Others can not get write permission.
 489         * x flags for directories are handled separately.
 490         */
 491        return -(i & 0666);
 492}
 493
 494int check_repository_format_version(const char *var, const char *value, void *cb)
 495{
 496        if (strcmp(var, "core.repositoryformatversion") == 0)
 497                repository_format_version = git_config_int(var, value);
 498        else if (strcmp(var, "core.sharedrepository") == 0)
 499                shared_repository = git_config_perm(var, value);
 500        else if (strcmp(var, "core.bare") == 0) {
 501                is_bare_repository_cfg = git_config_bool(var, value);
 502                if (is_bare_repository_cfg == 1)
 503                        inside_work_tree = -1;
 504        } else if (strcmp(var, "core.worktree") == 0) {
 505                if (!value)
 506                        return config_error_nonbool(var);
 507                free(git_work_tree_cfg);
 508                git_work_tree_cfg = xstrdup(value);
 509                inside_work_tree = -1;
 510        }
 511        return 0;
 512}
 513
 514int check_repository_format(void)
 515{
 516        return check_repository_format_gently(NULL);
 517}
 518
 519const char *setup_git_directory(void)
 520{
 521        const char *retval = setup_git_directory_gently(NULL);
 522
 523        /* If the work tree is not the default one, recompute prefix */
 524        if (inside_work_tree < 0) {
 525                static char buffer[PATH_MAX + 1];
 526                char *rel;
 527                if (retval && chdir(retval))
 528                        die_errno ("Could not jump back into original cwd");
 529                rel = get_relative_cwd(buffer, PATH_MAX, get_git_work_tree());
 530                if (rel && *rel && chdir(get_git_work_tree()))
 531                        die_errno ("Could not jump to working directory");
 532                return rel && *rel ? strcat(rel, "/") : NULL;
 533        }
 534
 535        return retval;
 536}