builtin-init-db.con commit Merge branch 'jn/web' (6295ac3)
   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
   9#ifndef DEFAULT_GIT_TEMPLATE_DIR
  10#define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates/"
  11#endif
  12
  13static void safe_create_dir(const char *dir, int share)
  14{
  15        if (mkdir(dir, 0777) < 0) {
  16                if (errno != EEXIST) {
  17                        perror(dir);
  18                        exit(1);
  19                }
  20        }
  21        else if (share && adjust_shared_perm(dir))
  22                die("Could not make %s writable by group\n", dir);
  23}
  24
  25static int copy_file(const char *dst, const char *src, int mode)
  26{
  27        int fdi, fdo, status;
  28
  29        mode = (mode & 0111) ? 0777 : 0666;
  30        if ((fdi = open(src, O_RDONLY)) < 0)
  31                return fdi;
  32        if ((fdo = open(dst, O_WRONLY | O_CREAT | O_EXCL, mode)) < 0) {
  33                close(fdi);
  34                return fdo;
  35        }
  36        status = copy_fd(fdi, fdo);
  37        close(fdo);
  38
  39        if (!status && adjust_shared_perm(dst))
  40                return -1;
  41
  42        return status;
  43}
  44
  45static void copy_templates_1(char *path, int baselen,
  46                             char *template, int template_baselen,
  47                             DIR *dir)
  48{
  49        struct dirent *de;
  50
  51        /* Note: if ".git/hooks" file exists in the repository being
  52         * re-initialized, /etc/core-git/templates/hooks/update would
  53         * cause git-init-db to fail here.  I think this is sane but
  54         * it means that the set of templates we ship by default, along
  55         * with the way the namespace under .git/ is organized, should
  56         * be really carefully chosen.
  57         */
  58        safe_create_dir(path, 1);
  59        while ((de = readdir(dir)) != NULL) {
  60                struct stat st_git, st_template;
  61                int namelen;
  62                int exists = 0;
  63
  64                if (de->d_name[0] == '.')
  65                        continue;
  66                namelen = strlen(de->d_name);
  67                if ((PATH_MAX <= baselen + namelen) ||
  68                    (PATH_MAX <= template_baselen + namelen))
  69                        die("insanely long template name %s", de->d_name);
  70                memcpy(path + baselen, de->d_name, namelen+1);
  71                memcpy(template + template_baselen, de->d_name, namelen+1);
  72                if (lstat(path, &st_git)) {
  73                        if (errno != ENOENT)
  74                                die("cannot stat %s", path);
  75                }
  76                else
  77                        exists = 1;
  78
  79                if (lstat(template, &st_template))
  80                        die("cannot stat template %s", template);
  81
  82                if (S_ISDIR(st_template.st_mode)) {
  83                        DIR *subdir = opendir(template);
  84                        int baselen_sub = baselen + namelen;
  85                        int template_baselen_sub = template_baselen + namelen;
  86                        if (!subdir)
  87                                die("cannot opendir %s", template);
  88                        path[baselen_sub++] =
  89                                template[template_baselen_sub++] = '/';
  90                        path[baselen_sub] =
  91                                template[template_baselen_sub] = 0;
  92                        copy_templates_1(path, baselen_sub,
  93                                         template, template_baselen_sub,
  94                                         subdir);
  95                        closedir(subdir);
  96                }
  97                else if (exists)
  98                        continue;
  99                else if (S_ISLNK(st_template.st_mode)) {
 100                        char lnk[256];
 101                        int len;
 102                        len = readlink(template, lnk, sizeof(lnk));
 103                        if (len < 0)
 104                                die("cannot readlink %s", template);
 105                        if (sizeof(lnk) <= len)
 106                                die("insanely long symlink %s", template);
 107                        lnk[len] = 0;
 108                        if (symlink(lnk, path))
 109                                die("cannot symlink %s %s", lnk, path);
 110                }
 111                else if (S_ISREG(st_template.st_mode)) {
 112                        if (copy_file(path, template, st_template.st_mode))
 113                                die("cannot copy %s to %s", template, path);
 114                }
 115                else
 116                        error("ignoring template %s", template);
 117        }
 118}
 119
 120static void copy_templates(const char *git_dir, int len, const char *template_dir)
 121{
 122        char path[PATH_MAX];
 123        char template_path[PATH_MAX];
 124        int template_len;
 125        DIR *dir;
 126
 127        if (!template_dir)
 128                template_dir = DEFAULT_GIT_TEMPLATE_DIR;
 129        strcpy(template_path, template_dir);
 130        template_len = strlen(template_path);
 131        if (template_path[template_len-1] != '/') {
 132                template_path[template_len++] = '/';
 133                template_path[template_len] = 0;
 134        }
 135        dir = opendir(template_path);
 136        if (!dir) {
 137                fprintf(stderr, "warning: templates not found %s\n",
 138                        template_dir);
 139                return;
 140        }
 141
 142        /* Make sure that template is from the correct vintage */
 143        strcpy(template_path + template_len, "config");
 144        repository_format_version = 0;
 145        git_config_from_file(check_repository_format_version,
 146                             template_path);
 147        template_path[template_len] = 0;
 148
 149        if (repository_format_version &&
 150            repository_format_version != GIT_REPO_VERSION) {
 151                fprintf(stderr, "warning: not copying templates of "
 152                        "a wrong format version %d from '%s'\n",
 153                        repository_format_version,
 154                        template_dir);
 155                closedir(dir);
 156                return;
 157        }
 158
 159        memcpy(path, git_dir, len);
 160        path[len] = 0;
 161        copy_templates_1(path, len,
 162                         template_path, template_len,
 163                         dir);
 164        closedir(dir);
 165}
 166
 167static void create_default_files(const char *git_dir, const char *template_path)
 168{
 169        unsigned len = strlen(git_dir);
 170        static char path[PATH_MAX];
 171        unsigned char sha1[20];
 172        struct stat st1;
 173        char repo_version_string[10];
 174
 175        if (len > sizeof(path)-50)
 176                die("insane git directory %s", git_dir);
 177        memcpy(path, git_dir, len);
 178
 179        if (len && path[len-1] != '/')
 180                path[len++] = '/';
 181
 182        /*
 183         * Create .git/refs/{heads,tags}
 184         */
 185        strcpy(path + len, "refs");
 186        safe_create_dir(path, 1);
 187        strcpy(path + len, "refs/heads");
 188        safe_create_dir(path, 1);
 189        strcpy(path + len, "refs/tags");
 190        safe_create_dir(path, 1);
 191
 192        /* First copy the templates -- we might have the default
 193         * config file there, in which case we would want to read
 194         * from it after installing.
 195         */
 196        path[len] = 0;
 197        copy_templates(path, len, template_path);
 198
 199        git_config(git_default_config);
 200
 201        /*
 202         * We would have created the above under user's umask -- under
 203         * shared-repository settings, we would need to fix them up.
 204         */
 205        if (shared_repository) {
 206                path[len] = 0;
 207                adjust_shared_perm(path);
 208                strcpy(path + len, "refs");
 209                adjust_shared_perm(path);
 210                strcpy(path + len, "refs/heads");
 211                adjust_shared_perm(path);
 212                strcpy(path + len, "refs/tags");
 213                adjust_shared_perm(path);
 214        }
 215
 216        /*
 217         * Create the default symlink from ".git/HEAD" to the "master"
 218         * branch, if it does not exist yet.
 219         */
 220        strcpy(path + len, "HEAD");
 221        if (read_ref(path, sha1) < 0) {
 222                if (create_symref(path, "refs/heads/master") < 0)
 223                        exit(1);
 224        }
 225
 226        /* This forces creation of new config file */
 227        sprintf(repo_version_string, "%d", GIT_REPO_VERSION);
 228        git_config_set("core.repositoryformatversion", repo_version_string);
 229
 230        path[len] = 0;
 231        strcpy(path + len, "config");
 232
 233        /* Check filemode trustability */
 234        if (!lstat(path, &st1)) {
 235                struct stat st2;
 236                int filemode = (!chmod(path, st1.st_mode ^ S_IXUSR) &&
 237                                !lstat(path, &st2) &&
 238                                st1.st_mode != st2.st_mode);
 239                git_config_set("core.filemode",
 240                               filemode ? "true" : "false");
 241        }
 242}
 243
 244static const char init_db_usage[] =
 245"git-init-db [--template=<template-directory>] [--shared]";
 246
 247/*
 248 * If you want to, you can share the DB area with any number of branches.
 249 * That has advantages: you can save space by sharing all the SHA1 objects.
 250 * On the other hand, it might just make lookup slower and messier. You
 251 * be the judge.  The default case is to have one DB per managed directory.
 252 */
 253int cmd_init_db(int argc, const char **argv, const char *prefix)
 254{
 255        const char *git_dir;
 256        const char *sha1_dir;
 257        const char *template_dir = NULL;
 258        char *path;
 259        int len, i;
 260
 261        for (i = 1; i < argc; i++, argv++) {
 262                const char *arg = argv[1];
 263                if (!strncmp(arg, "--template=", 11))
 264                        template_dir = arg+11;
 265                else if (!strcmp(arg, "--shared"))
 266                        shared_repository = PERM_GROUP;
 267                else if (!strncmp(arg, "--shared=", 9))
 268                        shared_repository = git_config_perm("arg", arg+9);
 269                else
 270                        usage(init_db_usage);
 271        }
 272
 273        /*
 274         * Set up the default .git directory contents
 275         */
 276        git_dir = getenv(GIT_DIR_ENVIRONMENT);
 277        if (!git_dir) {
 278                git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
 279                fprintf(stderr, "defaulting to local storage area\n");
 280        }
 281        safe_create_dir(git_dir, 0);
 282
 283        /* Check to see if the repository version is right.
 284         * Note that a newly created repository does not have
 285         * config file, so this will not fail.  What we are catching
 286         * is an attempt to reinitialize new repository with an old tool.
 287         */
 288        check_repository_format();
 289
 290        create_default_files(git_dir, template_dir);
 291
 292        /*
 293         * And set up the object store.
 294         */
 295        sha1_dir = get_object_directory();
 296        len = strlen(sha1_dir);
 297        path = xmalloc(len + 40);
 298        memcpy(path, sha1_dir, len);
 299
 300        safe_create_dir(sha1_dir, 1);
 301        strcpy(path+len, "/pack");
 302        safe_create_dir(path, 1);
 303        strcpy(path+len, "/info");
 304        safe_create_dir(path, 1);
 305
 306        if (shared_repository) {
 307                char buf[10];
 308                /* We do not spell "group" and such, so that
 309                 * the configuration can be read by older version
 310                 * of git.
 311                 */
 312                sprintf(buf, "%d", shared_repository);
 313                git_config_set("core.sharedrepository", buf);
 314        }
 315
 316        return 0;
 317}