builtin / init-db.con commit init: don't set core.worktree when initializing /.git (84ccad8)
   1/*
   2 * GIT - The information manager from hell
   3 *
   4 * Copyright (C) Linus Torvalds, 2005
   5 */
   6#include "cache.h"
   7#include "builtin.h"
   8#include "exec_cmd.h"
   9#include "parse-options.h"
  10
  11#ifndef DEFAULT_GIT_TEMPLATE_DIR
  12#define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates"
  13#endif
  14
  15#ifdef NO_TRUSTABLE_FILEMODE
  16#define TEST_FILEMODE 0
  17#else
  18#define TEST_FILEMODE 1
  19#endif
  20
  21static int init_is_bare_repository = 0;
  22static int init_shared_repository = -1;
  23static const char *init_db_template_dir;
  24static const char *git_link;
  25
  26static void safe_create_dir(const char *dir, int share)
  27{
  28        if (mkdir(dir, 0777) < 0) {
  29                if (errno != EEXIST) {
  30                        perror(dir);
  31                        exit(1);
  32                }
  33        }
  34        else if (share && adjust_shared_perm(dir))
  35                die(_("Could not make %s writable by group"), dir);
  36}
  37
  38static void copy_templates_1(char *path, int baselen,
  39                             char *template, int template_baselen,
  40                             DIR *dir)
  41{
  42        struct dirent *de;
  43
  44        /* Note: if ".git/hooks" file exists in the repository being
  45         * re-initialized, /etc/core-git/templates/hooks/update would
  46         * cause "git init" to fail here.  I think this is sane but
  47         * it means that the set of templates we ship by default, along
  48         * with the way the namespace under .git/ is organized, should
  49         * be really carefully chosen.
  50         */
  51        safe_create_dir(path, 1);
  52        while ((de = readdir(dir)) != NULL) {
  53                struct stat st_git, st_template;
  54                int namelen;
  55                int exists = 0;
  56
  57                if (de->d_name[0] == '.')
  58                        continue;
  59                namelen = strlen(de->d_name);
  60                if ((PATH_MAX <= baselen + namelen) ||
  61                    (PATH_MAX <= template_baselen + namelen))
  62                        die(_("insanely long template name %s"), de->d_name);
  63                memcpy(path + baselen, de->d_name, namelen+1);
  64                memcpy(template + template_baselen, de->d_name, namelen+1);
  65                if (lstat(path, &st_git)) {
  66                        if (errno != ENOENT)
  67                                die_errno(_("cannot stat '%s'"), path);
  68                }
  69                else
  70                        exists = 1;
  71
  72                if (lstat(template, &st_template))
  73                        die_errno(_("cannot stat template '%s'"), template);
  74
  75                if (S_ISDIR(st_template.st_mode)) {
  76                        DIR *subdir = opendir(template);
  77                        int baselen_sub = baselen + namelen;
  78                        int template_baselen_sub = template_baselen + namelen;
  79                        if (!subdir)
  80                                die_errno(_("cannot opendir '%s'"), template);
  81                        path[baselen_sub++] =
  82                                template[template_baselen_sub++] = '/';
  83                        path[baselen_sub] =
  84                                template[template_baselen_sub] = 0;
  85                        copy_templates_1(path, baselen_sub,
  86                                         template, template_baselen_sub,
  87                                         subdir);
  88                        closedir(subdir);
  89                }
  90                else if (exists)
  91                        continue;
  92                else if (S_ISLNK(st_template.st_mode)) {
  93                        char lnk[256];
  94                        int len;
  95                        len = readlink(template, lnk, sizeof(lnk));
  96                        if (len < 0)
  97                                die_errno(_("cannot readlink '%s'"), template);
  98                        if (sizeof(lnk) <= len)
  99                                die(_("insanely long symlink %s"), template);
 100                        lnk[len] = 0;
 101                        if (symlink(lnk, path))
 102                                die_errno(_("cannot symlink '%s' '%s'"), lnk, path);
 103                }
 104                else if (S_ISREG(st_template.st_mode)) {
 105                        if (copy_file(path, template, st_template.st_mode))
 106                                die_errno(_("cannot copy '%s' to '%s'"), template,
 107                                          path);
 108                }
 109                else
 110                        error(_("ignoring template %s"), template);
 111        }
 112}
 113
 114static void copy_templates(const char *template_dir)
 115{
 116        char path[PATH_MAX];
 117        char template_path[PATH_MAX];
 118        int template_len;
 119        DIR *dir;
 120        const char *git_dir = get_git_dir();
 121        int len = strlen(git_dir);
 122
 123        if (!template_dir)
 124                template_dir = getenv(TEMPLATE_DIR_ENVIRONMENT);
 125        if (!template_dir)
 126                template_dir = init_db_template_dir;
 127        if (!template_dir)
 128                template_dir = system_path(DEFAULT_GIT_TEMPLATE_DIR);
 129        if (!template_dir[0])
 130                return;
 131        template_len = strlen(template_dir);
 132        if (PATH_MAX <= (template_len+strlen("/config")))
 133                die(_("insanely long template path %s"), template_dir);
 134        strcpy(template_path, template_dir);
 135        if (template_path[template_len-1] != '/') {
 136                template_path[template_len++] = '/';
 137                template_path[template_len] = 0;
 138        }
 139        dir = opendir(template_path);
 140        if (!dir) {
 141                warning(_("templates not found %s"), template_dir);
 142                return;
 143        }
 144
 145        /* Make sure that template is from the correct vintage */
 146        strcpy(template_path + template_len, "config");
 147        repository_format_version = 0;
 148        git_config_from_file(check_repository_format_version,
 149                             template_path, NULL);
 150        template_path[template_len] = 0;
 151
 152        if (repository_format_version &&
 153            repository_format_version != GIT_REPO_VERSION) {
 154                warning(_("not copying templates of "
 155                        "a wrong format version %d from '%s'"),
 156                        repository_format_version,
 157                        template_dir);
 158                closedir(dir);
 159                return;
 160        }
 161
 162        memcpy(path, git_dir, len);
 163        if (len && path[len - 1] != '/')
 164                path[len++] = '/';
 165        path[len] = 0;
 166        copy_templates_1(path, len,
 167                         template_path, template_len,
 168                         dir);
 169        closedir(dir);
 170}
 171
 172static int git_init_db_config(const char *k, const char *v, void *cb)
 173{
 174        if (!strcmp(k, "init.templatedir"))
 175                return git_config_pathname(&init_db_template_dir, k, v);
 176
 177        return 0;
 178}
 179
 180/*
 181 * If the git_dir is not directly inside the working tree, then git will not
 182 * find it by default, and we need to set the worktree explicitly.
 183 */
 184static int needs_work_tree_config(const char *git_dir, const char *work_tree)
 185{
 186        if (!strcmp(work_tree, "/") && !strcmp(git_dir, "/.git"))
 187                return 0;
 188        if (skip_prefix(git_dir, work_tree, &git_dir) &&
 189            !strcmp(git_dir, "/.git"))
 190                return 0;
 191        return 1;
 192}
 193
 194static int create_default_files(const char *template_path)
 195{
 196        const char *git_dir = get_git_dir();
 197        unsigned len = strlen(git_dir);
 198        static char path[PATH_MAX];
 199        struct stat st1;
 200        char repo_version_string[10];
 201        char junk[2];
 202        int reinit;
 203        int filemode;
 204
 205        if (len > sizeof(path)-50)
 206                die(_("insane git directory %s"), git_dir);
 207        memcpy(path, git_dir, len);
 208
 209        if (len && path[len-1] != '/')
 210                path[len++] = '/';
 211
 212        /*
 213         * Create .git/refs/{heads,tags}
 214         */
 215        safe_create_dir(git_path("refs"), 1);
 216        safe_create_dir(git_path("refs/heads"), 1);
 217        safe_create_dir(git_path("refs/tags"), 1);
 218
 219        /* Just look for `init.templatedir` */
 220        git_config(git_init_db_config, NULL);
 221
 222        /* First copy the templates -- we might have the default
 223         * config file there, in which case we would want to read
 224         * from it after installing.
 225         */
 226        copy_templates(template_path);
 227
 228        git_config(git_default_config, NULL);
 229        is_bare_repository_cfg = init_is_bare_repository;
 230
 231        /* reading existing config may have overwrote it */
 232        if (init_shared_repository != -1)
 233                shared_repository = init_shared_repository;
 234
 235        /*
 236         * We would have created the above under user's umask -- under
 237         * shared-repository settings, we would need to fix them up.
 238         */
 239        if (shared_repository) {
 240                adjust_shared_perm(get_git_dir());
 241                adjust_shared_perm(git_path("refs"));
 242                adjust_shared_perm(git_path("refs/heads"));
 243                adjust_shared_perm(git_path("refs/tags"));
 244        }
 245
 246        /*
 247         * Create the default symlink from ".git/HEAD" to the "master"
 248         * branch, if it does not exist yet.
 249         */
 250        strcpy(path + len, "HEAD");
 251        reinit = (!access(path, R_OK)
 252                  || readlink(path, junk, sizeof(junk)-1) != -1);
 253        if (!reinit) {
 254                if (create_symref("HEAD", "refs/heads/master", NULL) < 0)
 255                        exit(1);
 256        }
 257
 258        /* This forces creation of new config file */
 259        sprintf(repo_version_string, "%d", GIT_REPO_VERSION);
 260        git_config_set("core.repositoryformatversion", repo_version_string);
 261
 262        path[len] = 0;
 263        strcpy(path + len, "config");
 264
 265        /* Check filemode trustability */
 266        filemode = TEST_FILEMODE;
 267        if (TEST_FILEMODE && !lstat(path, &st1)) {
 268                struct stat st2;
 269                filemode = (!chmod(path, st1.st_mode ^ S_IXUSR) &&
 270                                !lstat(path, &st2) &&
 271                                st1.st_mode != st2.st_mode);
 272        }
 273        git_config_set("core.filemode", filemode ? "true" : "false");
 274
 275        if (is_bare_repository())
 276                git_config_set("core.bare", "true");
 277        else {
 278                const char *work_tree = get_git_work_tree();
 279                git_config_set("core.bare", "false");
 280                /* allow template config file to override the default */
 281                if (log_all_ref_updates == -1)
 282                    git_config_set("core.logallrefupdates", "true");
 283                if (needs_work_tree_config(git_dir, work_tree))
 284                        git_config_set("core.worktree", work_tree);
 285        }
 286
 287        if (!reinit) {
 288                /* Check if symlink is supported in the work tree */
 289                path[len] = 0;
 290                strcpy(path + len, "tXXXXXX");
 291                if (!close(xmkstemp(path)) &&
 292                    !unlink(path) &&
 293                    !symlink("testing", path) &&
 294                    !lstat(path, &st1) &&
 295                    S_ISLNK(st1.st_mode))
 296                        unlink(path); /* good */
 297                else
 298                        git_config_set("core.symlinks", "false");
 299
 300                /* Check if the filesystem is case-insensitive */
 301                path[len] = 0;
 302                strcpy(path + len, "CoNfIg");
 303                if (!access(path, F_OK))
 304                        git_config_set("core.ignorecase", "true");
 305                probe_utf8_pathname_composition(path, len);
 306        }
 307
 308        return reinit;
 309}
 310
 311static void create_object_directory(void)
 312{
 313        const char *object_directory = get_object_directory();
 314        int len = strlen(object_directory);
 315        char *path = xmalloc(len + 40);
 316
 317        memcpy(path, object_directory, len);
 318
 319        safe_create_dir(object_directory, 1);
 320        strcpy(path+len, "/pack");
 321        safe_create_dir(path, 1);
 322        strcpy(path+len, "/info");
 323        safe_create_dir(path, 1);
 324
 325        free(path);
 326}
 327
 328int set_git_dir_init(const char *git_dir, const char *real_git_dir,
 329                     int exist_ok)
 330{
 331        if (real_git_dir) {
 332                struct stat st;
 333
 334                if (!exist_ok && !stat(git_dir, &st))
 335                        die(_("%s already exists"), git_dir);
 336
 337                if (!exist_ok && !stat(real_git_dir, &st))
 338                        die(_("%s already exists"), real_git_dir);
 339
 340                /*
 341                 * make sure symlinks are resolved because we'll be
 342                 * moving the target repo later on in separate_git_dir()
 343                 */
 344                git_link = xstrdup(real_path(git_dir));
 345        }
 346        else {
 347                real_git_dir = real_path(git_dir);
 348                git_link = NULL;
 349        }
 350        set_git_dir(real_path(real_git_dir));
 351        return 0;
 352}
 353
 354static void separate_git_dir(const char *git_dir)
 355{
 356        struct stat st;
 357        FILE *fp;
 358
 359        if (!stat(git_link, &st)) {
 360                const char *src;
 361
 362                if (S_ISREG(st.st_mode))
 363                        src = read_gitfile(git_link);
 364                else if (S_ISDIR(st.st_mode))
 365                        src = git_link;
 366                else
 367                        die(_("unable to handle file type %d"), (int)st.st_mode);
 368
 369                if (rename(src, git_dir))
 370                        die_errno(_("unable to move %s to %s"), src, git_dir);
 371        }
 372
 373        fp = fopen(git_link, "w");
 374        if (!fp)
 375                die(_("Could not create git link %s"), git_link);
 376        fprintf(fp, "gitdir: %s\n", git_dir);
 377        fclose(fp);
 378}
 379
 380int init_db(const char *template_dir, unsigned int flags)
 381{
 382        int reinit;
 383        const char *git_dir = get_git_dir();
 384
 385        if (git_link)
 386                separate_git_dir(git_dir);
 387
 388        safe_create_dir(git_dir, 0);
 389
 390        init_is_bare_repository = is_bare_repository();
 391
 392        /* Check to see if the repository version is right.
 393         * Note that a newly created repository does not have
 394         * config file, so this will not fail.  What we are catching
 395         * is an attempt to reinitialize new repository with an old tool.
 396         */
 397        check_repository_format();
 398
 399        reinit = create_default_files(template_dir);
 400
 401        create_object_directory();
 402
 403        if (shared_repository) {
 404                char buf[10];
 405                /* We do not spell "group" and such, so that
 406                 * the configuration can be read by older version
 407                 * of git. Note, we use octal numbers for new share modes,
 408                 * and compatibility values for PERM_GROUP and
 409                 * PERM_EVERYBODY.
 410                 */
 411                if (shared_repository < 0)
 412                        /* force to the mode value */
 413                        sprintf(buf, "0%o", -shared_repository);
 414                else if (shared_repository == PERM_GROUP)
 415                        sprintf(buf, "%d", OLD_PERM_GROUP);
 416                else if (shared_repository == PERM_EVERYBODY)
 417                        sprintf(buf, "%d", OLD_PERM_EVERYBODY);
 418                else
 419                        die("oops");
 420                git_config_set("core.sharedrepository", buf);
 421                git_config_set("receive.denyNonFastforwards", "true");
 422        }
 423
 424        if (!(flags & INIT_DB_QUIET)) {
 425                int len = strlen(git_dir);
 426
 427                /* TRANSLATORS: The first '%s' is either "Reinitialized
 428                   existing" or "Initialized empty", the second " shared" or
 429                   "", and the last '%s%s' is the verbatim directory name. */
 430                printf(_("%s%s Git repository in %s%s\n"),
 431                       reinit ? _("Reinitialized existing") : _("Initialized empty"),
 432                       shared_repository ? _(" shared") : "",
 433                       git_dir, len && git_dir[len-1] != '/' ? "/" : "");
 434        }
 435
 436        return 0;
 437}
 438
 439static int guess_repository_type(const char *git_dir)
 440{
 441        char cwd[PATH_MAX];
 442        const char *slash;
 443
 444        /*
 445         * "GIT_DIR=. git init" is always bare.
 446         * "GIT_DIR=`pwd` git init" too.
 447         */
 448        if (!strcmp(".", git_dir))
 449                return 1;
 450        if (!getcwd(cwd, sizeof(cwd)))
 451                die_errno(_("cannot tell cwd"));
 452        if (!strcmp(git_dir, cwd))
 453                return 1;
 454        /*
 455         * "GIT_DIR=.git or GIT_DIR=something/.git is usually not.
 456         */
 457        if (!strcmp(git_dir, ".git"))
 458                return 0;
 459        slash = strrchr(git_dir, '/');
 460        if (slash && !strcmp(slash, "/.git"))
 461                return 0;
 462
 463        /*
 464         * Otherwise it is often bare.  At this point
 465         * we are just guessing.
 466         */
 467        return 1;
 468}
 469
 470static int shared_callback(const struct option *opt, const char *arg, int unset)
 471{
 472        *((int *) opt->value) = (arg) ? git_config_perm("arg", arg) : PERM_GROUP;
 473        return 0;
 474}
 475
 476static const char *const init_db_usage[] = {
 477        N_("git init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]] [directory]"),
 478        NULL
 479};
 480
 481/*
 482 * If you want to, you can share the DB area with any number of branches.
 483 * That has advantages: you can save space by sharing all the SHA1 objects.
 484 * On the other hand, it might just make lookup slower and messier. You
 485 * be the judge.  The default case is to have one DB per managed directory.
 486 */
 487int cmd_init_db(int argc, const char **argv, const char *prefix)
 488{
 489        const char *git_dir;
 490        const char *real_git_dir = NULL;
 491        const char *work_tree;
 492        const char *template_dir = NULL;
 493        unsigned int flags = 0;
 494        const struct option init_db_options[] = {
 495                OPT_STRING(0, "template", &template_dir, N_("template-directory"),
 496                                N_("directory from which templates will be used")),
 497                OPT_SET_INT(0, "bare", &is_bare_repository_cfg,
 498                                N_("create a bare repository"), 1),
 499                { OPTION_CALLBACK, 0, "shared", &init_shared_repository,
 500                        N_("permissions"),
 501                        N_("specify that the git repository is to be shared amongst several users"),
 502                        PARSE_OPT_OPTARG | PARSE_OPT_NONEG, shared_callback, 0},
 503                OPT_BIT('q', "quiet", &flags, N_("be quiet"), INIT_DB_QUIET),
 504                OPT_STRING(0, "separate-git-dir", &real_git_dir, N_("gitdir"),
 505                           N_("separate git dir from working tree")),
 506                OPT_END()
 507        };
 508
 509        argc = parse_options(argc, argv, prefix, init_db_options, init_db_usage, 0);
 510
 511        if (real_git_dir && !is_absolute_path(real_git_dir))
 512                real_git_dir = xstrdup(real_path(real_git_dir));
 513
 514        if (argc == 1) {
 515                int mkdir_tried = 0;
 516        retry:
 517                if (chdir(argv[0]) < 0) {
 518                        if (!mkdir_tried) {
 519                                int saved;
 520                                /*
 521                                 * At this point we haven't read any configuration,
 522                                 * and we know shared_repository should always be 0;
 523                                 * but just in case we play safe.
 524                                 */
 525                                saved = shared_repository;
 526                                shared_repository = 0;
 527                                switch (safe_create_leading_directories_const(argv[0])) {
 528                                case SCLD_OK:
 529                                case SCLD_PERMS:
 530                                        break;
 531                                case SCLD_EXISTS:
 532                                        errno = EEXIST;
 533                                        /* fallthru */
 534                                default:
 535                                        die_errno(_("cannot mkdir %s"), argv[0]);
 536                                        break;
 537                                }
 538                                shared_repository = saved;
 539                                if (mkdir(argv[0], 0777) < 0)
 540                                        die_errno(_("cannot mkdir %s"), argv[0]);
 541                                mkdir_tried = 1;
 542                                goto retry;
 543                        }
 544                        die_errno(_("cannot chdir to %s"), argv[0]);
 545                }
 546        } else if (0 < argc) {
 547                usage(init_db_usage[0]);
 548        }
 549        if (is_bare_repository_cfg == 1) {
 550                static char git_dir[PATH_MAX+1];
 551
 552                setenv(GIT_DIR_ENVIRONMENT,
 553                        getcwd(git_dir, sizeof(git_dir)), argc > 0);
 554        }
 555
 556        if (init_shared_repository != -1)
 557                shared_repository = init_shared_repository;
 558
 559        /*
 560         * GIT_WORK_TREE makes sense only in conjunction with GIT_DIR
 561         * without --bare.  Catch the error early.
 562         */
 563        git_dir = getenv(GIT_DIR_ENVIRONMENT);
 564        work_tree = getenv(GIT_WORK_TREE_ENVIRONMENT);
 565        if ((!git_dir || is_bare_repository_cfg == 1) && work_tree)
 566                die(_("%s (or --work-tree=<directory>) not allowed without "
 567                          "specifying %s (or --git-dir=<directory>)"),
 568                    GIT_WORK_TREE_ENVIRONMENT,
 569                    GIT_DIR_ENVIRONMENT);
 570
 571        /*
 572         * Set up the default .git directory contents
 573         */
 574        if (!git_dir)
 575                git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
 576
 577        if (is_bare_repository_cfg < 0)
 578                is_bare_repository_cfg = guess_repository_type(git_dir);
 579
 580        if (!is_bare_repository_cfg) {
 581                const char *git_dir_parent = strrchr(git_dir, '/');
 582                if (git_dir_parent) {
 583                        char *rel = xstrndup(git_dir, git_dir_parent - git_dir);
 584                        git_work_tree_cfg = xstrdup(real_path(rel));
 585                        free(rel);
 586                }
 587                if (!git_work_tree_cfg) {
 588                        git_work_tree_cfg = xcalloc(PATH_MAX, 1);
 589                        if (!getcwd(git_work_tree_cfg, PATH_MAX))
 590                                die_errno (_("Cannot access current working directory"));
 591                }
 592                if (work_tree)
 593                        set_git_work_tree(real_path(work_tree));
 594                else
 595                        set_git_work_tree(git_work_tree_cfg);
 596                if (access(get_git_work_tree(), X_OK))
 597                        die_errno (_("Cannot access work tree '%s'"),
 598                                   get_git_work_tree());
 599        }
 600        else {
 601                if (work_tree)
 602                        set_git_work_tree(real_path(work_tree));
 603        }
 604
 605        set_git_dir_init(git_dir, real_git_dir, 1);
 606
 607        return init_db(template_dir, flags);
 608}