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