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