2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
11 #include "parse-options.h"
13 #ifndef DEFAULT_GIT_TEMPLATE_DIR
14 #define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates"
17 #ifdef NO_TRUSTABLE_FILEMODE
18 #define TEST_FILEMODE 0
20 #define TEST_FILEMODE 1
23 static int init_is_bare_repository = 0;
24 static int init_shared_repository = -1;
25 static const char *init_db_template_dir;
27 static void copy_templates_1(struct strbuf *path, struct strbuf *template_path,
30 size_t path_baselen = path->len;
31 size_t template_baselen = template_path->len;
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.
41 safe_create_dir(path->buf, 1);
42 while ((de = readdir(dir)) != NULL) {
43 struct stat st_git, st_template;
46 strbuf_setlen(path, path_baselen);
47 strbuf_setlen(template_path, template_baselen);
49 if (de->d_name[0] == '.')
51 strbuf_addstr(path, de->d_name);
52 strbuf_addstr(template_path, de->d_name);
53 if (lstat(path->buf, &st_git)) {
55 die_errno(_("cannot stat '%s'"), path->buf);
60 if (lstat(template_path->buf, &st_template))
61 die_errno(_("cannot stat template '%s'"), template_path->buf);
63 if (S_ISDIR(st_template.st_mode)) {
64 DIR *subdir = opendir(template_path->buf);
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);
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'"),
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);
90 error(_("ignoring template %s"), template_path->buf);
94 static void copy_templates(const char *template_dir)
96 struct strbuf path = STRBUF_INIT;
97 struct strbuf template_path = STRBUF_INIT;
99 struct repository_format template_format = REPOSITORY_FORMAT_INIT;
100 struct strbuf err = STRBUF_INIT;
102 char *to_free = NULL;
105 template_dir = getenv(TEMPLATE_DIR_ENVIRONMENT);
107 template_dir = init_db_template_dir;
109 template_dir = to_free = system_path(DEFAULT_GIT_TEMPLATE_DIR);
110 if (!template_dir[0]) {
115 strbuf_addstr(&template_path, template_dir);
116 strbuf_complete(&template_path, '/');
117 template_len = template_path.len;
119 dir = opendir(template_path.buf);
121 warning(_("templates not found in %s"), template_dir);
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);
131 * No mention of version at all is OK, but anything else should be
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;
142 strbuf_addstr(&path, get_git_common_dir());
143 strbuf_complete(&path, '/');
144 copy_templates_1(&path, &template_path, dir);
149 strbuf_release(&path);
150 strbuf_release(&template_path);
151 clear_repository_format(&template_format);
154 static int git_init_db_config(const char *k, const char *v, void *cb)
156 if (!strcmp(k, "init.templatedir"))
157 return git_config_pathname(&init_db_template_dir, k, v);
159 if (starts_with(k, "core."))
160 return platform_core_config(k, v, cb);
166 * If the git_dir is not directly inside the working tree, then git will not
167 * find it by default, and we need to set the worktree explicitly.
169 static int needs_work_tree_config(const char *git_dir, const char *work_tree)
171 if (!strcmp(work_tree, "/") && !strcmp(git_dir, "/.git"))
173 if (skip_prefix(git_dir, work_tree, &git_dir) &&
174 !strcmp(git_dir, "/.git"))
179 static int create_default_files(const char *template_path,
180 const char *original_git_dir,
181 const struct repository_format *fmt)
184 struct strbuf buf = STRBUF_INIT;
186 char repo_version_string[10];
190 struct strbuf err = STRBUF_INIT;
191 int repo_version = GIT_REPO_VERSION;
193 /* Just look for `init.templatedir` */
194 init_db_template_dir = NULL; /* re-set in case it was set before */
195 git_config(git_init_db_config, NULL);
198 * First copy the templates -- we might have the default
199 * config file there, in which case we would want to read
200 * from it after installing.
202 * Before reading that config, we also need to clear out any cached
203 * values (since we've just potentially changed what's available on
206 copy_templates(template_path);
208 reset_shared_repository();
209 git_config(git_default_config, NULL);
212 * We must make sure command-line options continue to override any
213 * values we might have just re-read from the config.
215 is_bare_repository_cfg = init_is_bare_repository;
216 if (init_shared_repository != -1)
217 set_shared_repository(init_shared_repository);
220 * We would have created the above under user's umask -- under
221 * shared-repository settings, we would need to fix them up.
223 if (get_shared_repository()) {
224 adjust_shared_perm(get_git_dir());
228 * We need to create a "refs" dir in any case so that older
229 * versions of git can tell that this is a repository.
231 safe_create_dir(git_path("refs"), 1);
232 adjust_shared_perm(git_path("refs"));
234 if (refs_init_db(&err))
235 die("failed to set up refs db: %s", err.buf);
238 * Create the default symlink from ".git/HEAD" to the "master"
239 * branch, if it does not exist yet.
241 path = git_path_buf(&buf, "HEAD");
242 reinit = (!access(path, R_OK)
243 || readlink(path, junk, sizeof(junk)-1) != -1);
245 if (create_symref("HEAD", "refs/heads/master", NULL) < 0)
249 #ifndef ENABLE_SHA256
250 if (fmt->hash_algo != GIT_HASH_SHA1)
251 die(_("The hash algorithm %s is not supported in this build."), hash_algos[fmt->hash_algo].name);
254 if (fmt->hash_algo != GIT_HASH_SHA1)
255 repo_version = GIT_REPO_VERSION_READ;
257 /* This forces creation of new config file */
258 xsnprintf(repo_version_string, sizeof(repo_version_string),
260 git_config_set("core.repositoryformatversion", repo_version_string);
262 if (fmt->hash_algo != GIT_HASH_SHA1)
263 git_config_set("extensions.objectformat",
264 hash_algos[fmt->hash_algo].name);
266 /* Check filemode trustability */
267 path = git_path_buf(&buf, "config");
268 filemode = TEST_FILEMODE;
269 if (TEST_FILEMODE && !lstat(path, &st1)) {
271 filemode = (!chmod(path, st1.st_mode ^ S_IXUSR) &&
272 !lstat(path, &st2) &&
273 st1.st_mode != st2.st_mode &&
274 !chmod(path, st1.st_mode));
275 if (filemode && !reinit && (st1.st_mode & S_IXUSR))
278 git_config_set("core.filemode", filemode ? "true" : "false");
280 if (is_bare_repository())
281 git_config_set("core.bare", "true");
283 const char *work_tree = get_git_work_tree();
284 git_config_set("core.bare", "false");
285 /* allow template config file to override the default */
286 if (log_all_ref_updates == LOG_REFS_UNSET)
287 git_config_set("core.logallrefupdates", "true");
288 if (needs_work_tree_config(original_git_dir, work_tree))
289 git_config_set("core.worktree", work_tree);
293 /* Check if symlink is supported in the work tree */
294 path = git_path_buf(&buf, "tXXXXXX");
295 if (!close(xmkstemp(path)) &&
297 !symlink("testing", path) &&
298 !lstat(path, &st1) &&
299 S_ISLNK(st1.st_mode))
300 unlink(path); /* good */
302 git_config_set("core.symlinks", "false");
304 /* Check if the filesystem is case-insensitive */
305 path = git_path_buf(&buf, "CoNfIg");
306 if (!access(path, F_OK))
307 git_config_set("core.ignorecase", "true");
308 probe_utf8_pathname_composition();
311 strbuf_release(&buf);
315 static void create_object_directory(void)
317 struct strbuf path = STRBUF_INIT;
320 strbuf_addstr(&path, get_object_directory());
323 safe_create_dir(path.buf, 1);
325 strbuf_setlen(&path, baselen);
326 strbuf_addstr(&path, "/pack");
327 safe_create_dir(path.buf, 1);
329 strbuf_setlen(&path, baselen);
330 strbuf_addstr(&path, "/info");
331 safe_create_dir(path.buf, 1);
333 strbuf_release(&path);
336 static void separate_git_dir(const char *git_dir, const char *git_link)
340 if (!stat(git_link, &st)) {
343 if (S_ISREG(st.st_mode))
344 src = read_gitfile(git_link);
345 else if (S_ISDIR(st.st_mode))
348 die(_("unable to handle file type %d"), (int)st.st_mode);
350 if (rename(src, git_dir))
351 die_errno(_("unable to move %s to %s"), src, git_dir);
354 write_file(git_link, "gitdir: %s", git_dir);
357 static void validate_hash_algorithm(struct repository_format *repo_fmt, int hash)
360 * If we already have an initialized repo, don't allow the user to
361 * specify a different algorithm, as that could cause corruption.
362 * Otherwise, if the user has specified one on the command line, use it.
364 if (repo_fmt->version >= 0 && hash != GIT_HASH_UNKNOWN && hash != repo_fmt->hash_algo)
365 die(_("attempt to reinitialize repository with different hash"));
366 else if (hash != GIT_HASH_UNKNOWN)
367 repo_fmt->hash_algo = hash;
370 int init_db(const char *git_dir, const char *real_git_dir,
371 const char *template_dir, int hash, unsigned int flags)
374 int exist_ok = flags & INIT_DB_EXIST_OK;
375 char *original_git_dir = real_pathdup(git_dir, 1);
376 struct repository_format repo_fmt = REPOSITORY_FORMAT_INIT;
381 if (!exist_ok && !stat(git_dir, &st))
382 die(_("%s already exists"), git_dir);
384 if (!exist_ok && !stat(real_git_dir, &st))
385 die(_("%s already exists"), real_git_dir);
387 set_git_dir(real_path(real_git_dir));
388 git_dir = get_git_dir();
389 separate_git_dir(git_dir, original_git_dir);
392 set_git_dir(real_path(git_dir));
393 git_dir = get_git_dir();
395 startup_info->have_repository = 1;
397 /* Just look for `core.hidedotfiles` */
398 git_config(git_init_db_config, NULL);
400 safe_create_dir(git_dir, 0);
402 init_is_bare_repository = is_bare_repository();
404 /* Check to see if the repository version is right.
405 * Note that a newly created repository does not have
406 * config file, so this will not fail. What we are catching
407 * is an attempt to reinitialize new repository with an old tool.
409 check_repository_format(&repo_fmt);
411 validate_hash_algorithm(&repo_fmt, hash);
413 reinit = create_default_files(template_dir, original_git_dir, &repo_fmt);
415 create_object_directory();
417 if (get_shared_repository()) {
419 /* We do not spell "group" and such, so that
420 * the configuration can be read by older version
421 * of git. Note, we use octal numbers for new share modes,
422 * and compatibility values for PERM_GROUP and
425 if (get_shared_repository() < 0)
426 /* force to the mode value */
427 xsnprintf(buf, sizeof(buf), "0%o", -get_shared_repository());
428 else if (get_shared_repository() == PERM_GROUP)
429 xsnprintf(buf, sizeof(buf), "%d", OLD_PERM_GROUP);
430 else if (get_shared_repository() == PERM_EVERYBODY)
431 xsnprintf(buf, sizeof(buf), "%d", OLD_PERM_EVERYBODY);
433 BUG("invalid value for shared_repository");
434 git_config_set("core.sharedrepository", buf);
435 git_config_set("receive.denyNonFastforwards", "true");
438 if (!(flags & INIT_DB_QUIET)) {
439 int len = strlen(git_dir);
442 printf(get_shared_repository()
443 ? _("Reinitialized existing shared Git repository in %s%s\n")
444 : _("Reinitialized existing Git repository in %s%s\n"),
445 git_dir, len && git_dir[len-1] != '/' ? "/" : "");
447 printf(get_shared_repository()
448 ? _("Initialized empty shared Git repository in %s%s\n")
449 : _("Initialized empty Git repository in %s%s\n"),
450 git_dir, len && git_dir[len-1] != '/' ? "/" : "");
453 free(original_git_dir);
457 static int guess_repository_type(const char *git_dir)
464 * "GIT_DIR=. git init" is always bare.
465 * "GIT_DIR=`pwd` git init" too.
467 if (!strcmp(".", git_dir))
470 cwd_is_git_dir = !strcmp(git_dir, cwd);
475 * "GIT_DIR=.git or GIT_DIR=something/.git is usually not.
477 if (!strcmp(git_dir, ".git"))
479 slash = strrchr(git_dir, '/');
480 if (slash && !strcmp(slash, "/.git"))
484 * Otherwise it is often bare. At this point
485 * we are just guessing.
490 static int shared_callback(const struct option *opt, const char *arg, int unset)
492 BUG_ON_OPT_NEG(unset);
493 *((int *) opt->value) = (arg) ? git_config_perm("arg", arg) : PERM_GROUP;
497 static const char *const init_db_usage[] = {
498 N_("git init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]] [<directory>]"),
503 * If you want to, you can share the DB area with any number of branches.
504 * That has advantages: you can save space by sharing all the SHA1 objects.
505 * On the other hand, it might just make lookup slower and messier. You
506 * be the judge. The default case is to have one DB per managed directory.
508 int cmd_init_db(int argc, const char **argv, const char *prefix)
511 const char *real_git_dir = NULL;
512 const char *work_tree;
513 const char *template_dir = NULL;
514 unsigned int flags = 0;
515 const char *object_format = NULL;
516 int hash_algo = GIT_HASH_UNKNOWN;
517 const struct option init_db_options[] = {
518 OPT_STRING(0, "template", &template_dir, N_("template-directory"),
519 N_("directory from which templates will be used")),
520 OPT_SET_INT(0, "bare", &is_bare_repository_cfg,
521 N_("create a bare repository"), 1),
522 { OPTION_CALLBACK, 0, "shared", &init_shared_repository,
524 N_("specify that the git repository is to be shared amongst several users"),
525 PARSE_OPT_OPTARG | PARSE_OPT_NONEG, shared_callback, 0},
526 OPT_BIT('q', "quiet", &flags, N_("be quiet"), INIT_DB_QUIET),
527 OPT_STRING(0, "separate-git-dir", &real_git_dir, N_("gitdir"),
528 N_("separate git dir from working tree")),
529 OPT_STRING(0, "object-format", &object_format, N_("hash"),
530 N_("specify the hash algorithm to use")),
534 argc = parse_options(argc, argv, prefix, init_db_options, init_db_usage, 0);
536 if (real_git_dir && !is_absolute_path(real_git_dir))
537 real_git_dir = real_pathdup(real_git_dir, 1);
539 if (template_dir && *template_dir && !is_absolute_path(template_dir))
540 template_dir = absolute_pathdup(template_dir);
545 if (chdir(argv[0]) < 0) {
549 * At this point we haven't read any configuration,
550 * and we know shared_repository should always be 0;
551 * but just in case we play safe.
553 saved = get_shared_repository();
554 set_shared_repository(0);
555 switch (safe_create_leading_directories_const(argv[0])) {
563 die_errno(_("cannot mkdir %s"), argv[0]);
566 set_shared_repository(saved);
567 if (mkdir(argv[0], 0777) < 0)
568 die_errno(_("cannot mkdir %s"), argv[0]);
572 die_errno(_("cannot chdir to %s"), argv[0]);
574 } else if (0 < argc) {
575 usage(init_db_usage[0]);
577 if (is_bare_repository_cfg == 1) {
578 char *cwd = xgetcwd();
579 setenv(GIT_DIR_ENVIRONMENT, cwd, argc > 0);
584 hash_algo = hash_algo_by_name(object_format);
585 if (hash_algo == GIT_HASH_UNKNOWN)
586 die(_("unknown hash algorithm '%s'"), object_format);
589 if (init_shared_repository != -1)
590 set_shared_repository(init_shared_repository);
593 * GIT_WORK_TREE makes sense only in conjunction with GIT_DIR
594 * without --bare. Catch the error early.
596 git_dir = xstrdup_or_null(getenv(GIT_DIR_ENVIRONMENT));
597 work_tree = xstrdup_or_null(getenv(GIT_WORK_TREE_ENVIRONMENT));
598 if ((!git_dir || is_bare_repository_cfg == 1) && work_tree)
599 die(_("%s (or --work-tree=<directory>) not allowed without "
600 "specifying %s (or --git-dir=<directory>)"),
601 GIT_WORK_TREE_ENVIRONMENT,
602 GIT_DIR_ENVIRONMENT);
605 * Set up the default .git directory contents
608 git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
610 if (is_bare_repository_cfg < 0)
611 is_bare_repository_cfg = guess_repository_type(git_dir);
613 if (!is_bare_repository_cfg) {
614 const char *git_dir_parent = strrchr(git_dir, '/');
615 if (git_dir_parent) {
616 char *rel = xstrndup(git_dir, git_dir_parent - git_dir);
617 git_work_tree_cfg = real_pathdup(rel, 1);
620 if (!git_work_tree_cfg)
621 git_work_tree_cfg = xgetcwd();
623 set_git_work_tree(work_tree);
625 set_git_work_tree(git_work_tree_cfg);
626 if (access(get_git_work_tree(), X_OK))
627 die_errno (_("Cannot access work tree '%s'"),
628 get_git_work_tree());
632 set_git_work_tree(work_tree);
635 UNLEAK(real_git_dir);
639 flags |= INIT_DB_EXIST_OK;
640 return init_db(git_dir, real_git_dir, template_dir, hash_algo, flags);