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;
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);
153 static int git_init_db_config(const char *k, const char *v, void *cb)
155 if (!strcmp(k, "init.templatedir"))
156 return git_config_pathname(&init_db_template_dir, k, v);
158 if (starts_with(k, "core."))
159 return platform_core_config(k, v, cb);
165 * If the git_dir is not directly inside the working tree, then git will not
166 * find it by default, and we need to set the worktree explicitly.
168 static int needs_work_tree_config(const char *git_dir, const char *work_tree)
170 if (!strcmp(work_tree, "/") && !strcmp(git_dir, "/.git"))
172 if (skip_prefix(git_dir, work_tree, &git_dir) &&
173 !strcmp(git_dir, "/.git"))
178 static int create_default_files(const char *template_path,
179 const char *original_git_dir)
182 struct strbuf buf = STRBUF_INIT;
184 char repo_version_string[10];
188 struct strbuf err = STRBUF_INIT;
190 /* Just look for `init.templatedir` */
191 init_db_template_dir = NULL; /* re-set in case it was set before */
192 git_config(git_init_db_config, NULL);
195 * First copy the templates -- we might have the default
196 * config file there, in which case we would want to read
197 * from it after installing.
199 * Before reading that config, we also need to clear out any cached
200 * values (since we've just potentially changed what's available on
203 copy_templates(template_path);
205 reset_shared_repository();
206 git_config(git_default_config, NULL);
209 * We must make sure command-line options continue to override any
210 * values we might have just re-read from the config.
212 is_bare_repository_cfg = init_is_bare_repository;
213 if (init_shared_repository != -1)
214 set_shared_repository(init_shared_repository);
217 * We would have created the above under user's umask -- under
218 * shared-repository settings, we would need to fix them up.
220 if (get_shared_repository()) {
221 adjust_shared_perm(get_git_dir());
225 * We need to create a "refs" dir in any case so that older
226 * versions of git can tell that this is a repository.
228 safe_create_dir(git_path("refs"), 1);
229 adjust_shared_perm(git_path("refs"));
231 if (refs_init_db(&err))
232 die("failed to set up refs db: %s", err.buf);
235 * Create the default symlink from ".git/HEAD" to the "master"
236 * branch, if it does not exist yet.
238 path = git_path_buf(&buf, "HEAD");
239 reinit = (!access(path, R_OK)
240 || readlink(path, junk, sizeof(junk)-1) != -1);
242 if (create_symref("HEAD", "refs/heads/master", NULL) < 0)
246 /* This forces creation of new config file */
247 xsnprintf(repo_version_string, sizeof(repo_version_string),
248 "%d", GIT_REPO_VERSION);
249 git_config_set("core.repositoryformatversion", repo_version_string);
251 /* Check filemode trustability */
252 path = git_path_buf(&buf, "config");
253 filemode = TEST_FILEMODE;
254 if (TEST_FILEMODE && !lstat(path, &st1)) {
256 filemode = (!chmod(path, st1.st_mode ^ S_IXUSR) &&
257 !lstat(path, &st2) &&
258 st1.st_mode != st2.st_mode &&
259 !chmod(path, st1.st_mode));
260 if (filemode && !reinit && (st1.st_mode & S_IXUSR))
263 git_config_set("core.filemode", filemode ? "true" : "false");
265 if (is_bare_repository())
266 git_config_set("core.bare", "true");
268 const char *work_tree = get_git_work_tree();
269 git_config_set("core.bare", "false");
270 /* allow template config file to override the default */
271 if (log_all_ref_updates == LOG_REFS_UNSET)
272 git_config_set("core.logallrefupdates", "true");
273 if (needs_work_tree_config(original_git_dir, work_tree))
274 git_config_set("core.worktree", work_tree);
278 /* Check if symlink is supported in the work tree */
279 path = git_path_buf(&buf, "tXXXXXX");
280 if (!close(xmkstemp(path)) &&
282 !symlink("testing", path) &&
283 !lstat(path, &st1) &&
284 S_ISLNK(st1.st_mode))
285 unlink(path); /* good */
287 git_config_set("core.symlinks", "false");
289 /* Check if the filesystem is case-insensitive */
290 path = git_path_buf(&buf, "CoNfIg");
291 if (!access(path, F_OK))
292 git_config_set("core.ignorecase", "true");
293 probe_utf8_pathname_composition();
296 strbuf_release(&buf);
300 static void create_object_directory(void)
302 struct strbuf path = STRBUF_INIT;
305 strbuf_addstr(&path, get_object_directory());
308 safe_create_dir(path.buf, 1);
310 strbuf_setlen(&path, baselen);
311 strbuf_addstr(&path, "/pack");
312 safe_create_dir(path.buf, 1);
314 strbuf_setlen(&path, baselen);
315 strbuf_addstr(&path, "/info");
316 safe_create_dir(path.buf, 1);
318 strbuf_release(&path);
321 static void separate_git_dir(const char *git_dir, const char *git_link)
325 if (!stat(git_link, &st)) {
328 if (S_ISREG(st.st_mode))
329 src = read_gitfile(git_link);
330 else if (S_ISDIR(st.st_mode))
333 die(_("unable to handle file type %d"), (int)st.st_mode);
335 if (rename(src, git_dir))
336 die_errno(_("unable to move %s to %s"), src, git_dir);
339 write_file(git_link, "gitdir: %s", git_dir);
342 int init_db(const char *git_dir, const char *real_git_dir,
343 const char *template_dir, unsigned int flags)
346 int exist_ok = flags & INIT_DB_EXIST_OK;
347 char *original_git_dir = real_pathdup(git_dir, 1);
352 if (!exist_ok && !stat(git_dir, &st))
353 die(_("%s already exists"), git_dir);
355 if (!exist_ok && !stat(real_git_dir, &st))
356 die(_("%s already exists"), real_git_dir);
358 set_git_dir(real_path(real_git_dir));
359 git_dir = get_git_dir();
360 separate_git_dir(git_dir, original_git_dir);
363 set_git_dir(real_path(git_dir));
364 git_dir = get_git_dir();
366 startup_info->have_repository = 1;
368 /* Just look for `core.hidedotfiles` */
369 git_config(git_init_db_config, NULL);
371 safe_create_dir(git_dir, 0);
373 init_is_bare_repository = is_bare_repository();
375 /* Check to see if the repository version is right.
376 * Note that a newly created repository does not have
377 * config file, so this will not fail. What we are catching
378 * is an attempt to reinitialize new repository with an old tool.
380 check_repository_format();
382 reinit = create_default_files(template_dir, original_git_dir);
384 create_object_directory();
386 if (get_shared_repository()) {
388 /* We do not spell "group" and such, so that
389 * the configuration can be read by older version
390 * of git. Note, we use octal numbers for new share modes,
391 * and compatibility values for PERM_GROUP and
394 if (get_shared_repository() < 0)
395 /* force to the mode value */
396 xsnprintf(buf, sizeof(buf), "0%o", -get_shared_repository());
397 else if (get_shared_repository() == PERM_GROUP)
398 xsnprintf(buf, sizeof(buf), "%d", OLD_PERM_GROUP);
399 else if (get_shared_repository() == PERM_EVERYBODY)
400 xsnprintf(buf, sizeof(buf), "%d", OLD_PERM_EVERYBODY);
402 BUG("invalid value for shared_repository");
403 git_config_set("core.sharedrepository", buf);
404 git_config_set("receive.denyNonFastforwards", "true");
407 if (!(flags & INIT_DB_QUIET)) {
408 int len = strlen(git_dir);
411 printf(get_shared_repository()
412 ? _("Reinitialized existing shared Git repository in %s%s\n")
413 : _("Reinitialized existing Git repository in %s%s\n"),
414 git_dir, len && git_dir[len-1] != '/' ? "/" : "");
416 printf(get_shared_repository()
417 ? _("Initialized empty shared Git repository in %s%s\n")
418 : _("Initialized empty Git repository in %s%s\n"),
419 git_dir, len && git_dir[len-1] != '/' ? "/" : "");
422 free(original_git_dir);
426 static int guess_repository_type(const char *git_dir)
433 * "GIT_DIR=. git init" is always bare.
434 * "GIT_DIR=`pwd` git init" too.
436 if (!strcmp(".", git_dir))
439 cwd_is_git_dir = !strcmp(git_dir, cwd);
444 * "GIT_DIR=.git or GIT_DIR=something/.git is usually not.
446 if (!strcmp(git_dir, ".git"))
448 slash = strrchr(git_dir, '/');
449 if (slash && !strcmp(slash, "/.git"))
453 * Otherwise it is often bare. At this point
454 * we are just guessing.
459 static int shared_callback(const struct option *opt, const char *arg, int unset)
461 BUG_ON_OPT_NEG(unset);
462 *((int *) opt->value) = (arg) ? git_config_perm("arg", arg) : PERM_GROUP;
466 static const char *const init_db_usage[] = {
467 N_("git init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]] [<directory>]"),
472 * If you want to, you can share the DB area with any number of branches.
473 * That has advantages: you can save space by sharing all the SHA1 objects.
474 * On the other hand, it might just make lookup slower and messier. You
475 * be the judge. The default case is to have one DB per managed directory.
477 int cmd_init_db(int argc, const char **argv, const char *prefix)
480 const char *real_git_dir = NULL;
481 const char *work_tree;
482 const char *template_dir = NULL;
483 unsigned int flags = 0;
484 const struct option init_db_options[] = {
485 OPT_STRING(0, "template", &template_dir, N_("template-directory"),
486 N_("directory from which templates will be used")),
487 OPT_SET_INT(0, "bare", &is_bare_repository_cfg,
488 N_("create a bare repository"), 1),
489 { OPTION_CALLBACK, 0, "shared", &init_shared_repository,
491 N_("specify that the git repository is to be shared amongst several users"),
492 PARSE_OPT_OPTARG | PARSE_OPT_NONEG, shared_callback, 0},
493 OPT_BIT('q', "quiet", &flags, N_("be quiet"), INIT_DB_QUIET),
494 OPT_STRING(0, "separate-git-dir", &real_git_dir, N_("gitdir"),
495 N_("separate git dir from working tree")),
499 argc = parse_options(argc, argv, prefix, init_db_options, init_db_usage, 0);
501 if (real_git_dir && !is_absolute_path(real_git_dir))
502 real_git_dir = real_pathdup(real_git_dir, 1);
507 if (chdir(argv[0]) < 0) {
511 * At this point we haven't read any configuration,
512 * and we know shared_repository should always be 0;
513 * but just in case we play safe.
515 saved = get_shared_repository();
516 set_shared_repository(0);
517 switch (safe_create_leading_directories_const(argv[0])) {
525 die_errno(_("cannot mkdir %s"), argv[0]);
528 set_shared_repository(saved);
529 if (mkdir(argv[0], 0777) < 0)
530 die_errno(_("cannot mkdir %s"), argv[0]);
534 die_errno(_("cannot chdir to %s"), argv[0]);
536 } else if (0 < argc) {
537 usage(init_db_usage[0]);
539 if (is_bare_repository_cfg == 1) {
540 char *cwd = xgetcwd();
541 setenv(GIT_DIR_ENVIRONMENT, cwd, argc > 0);
545 if (init_shared_repository != -1)
546 set_shared_repository(init_shared_repository);
549 * GIT_WORK_TREE makes sense only in conjunction with GIT_DIR
550 * without --bare. Catch the error early.
552 git_dir = xstrdup_or_null(getenv(GIT_DIR_ENVIRONMENT));
553 work_tree = xstrdup_or_null(getenv(GIT_WORK_TREE_ENVIRONMENT));
554 if ((!git_dir || is_bare_repository_cfg == 1) && work_tree)
555 die(_("%s (or --work-tree=<directory>) not allowed without "
556 "specifying %s (or --git-dir=<directory>)"),
557 GIT_WORK_TREE_ENVIRONMENT,
558 GIT_DIR_ENVIRONMENT);
561 * Set up the default .git directory contents
564 git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
566 if (is_bare_repository_cfg < 0)
567 is_bare_repository_cfg = guess_repository_type(git_dir);
569 if (!is_bare_repository_cfg) {
570 const char *git_dir_parent = strrchr(git_dir, '/');
571 if (git_dir_parent) {
572 char *rel = xstrndup(git_dir, git_dir_parent - git_dir);
573 git_work_tree_cfg = real_pathdup(rel, 1);
576 if (!git_work_tree_cfg)
577 git_work_tree_cfg = xgetcwd();
579 set_git_work_tree(work_tree);
581 set_git_work_tree(git_work_tree_cfg);
582 if (access(get_git_work_tree(), X_OK))
583 die_errno (_("Cannot access work tree '%s'"),
584 get_git_work_tree());
588 set_git_work_tree(work_tree);
591 UNLEAK(real_git_dir);
595 flags |= INIT_DB_EXIST_OK;
596 return init_db(git_dir, real_git_dir, template_dir, flags);