2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
10 #include "parse-options.h"
12 #ifndef DEFAULT_GIT_TEMPLATE_DIR
13 #define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates"
16 #ifdef NO_TRUSTABLE_FILEMODE
17 #define TEST_FILEMODE 0
19 #define TEST_FILEMODE 1
22 static int init_is_bare_repository = 0;
23 static int init_shared_repository = -1;
24 static const char *init_db_template_dir;
25 static const char *git_link;
27 static void copy_templates_1(struct strbuf *path, struct strbuf *template,
30 size_t path_baselen = path->len;
31 size_t template_baselen = template->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, template_baselen);
49 if (de->d_name[0] == '.')
51 strbuf_addstr(path, de->d_name);
52 strbuf_addstr(template, de->d_name);
53 if (lstat(path->buf, &st_git)) {
55 die_errno(_("cannot stat '%s'"), path->buf);
60 if (lstat(template->buf, &st_template))
61 die_errno(_("cannot stat template '%s'"), template->buf);
63 if (S_ISDIR(st_template.st_mode)) {
64 DIR *subdir = opendir(template->buf);
66 die_errno(_("cannot opendir '%s'"), template->buf);
67 strbuf_addch(path, '/');
68 strbuf_addch(template, '/');
69 copy_templates_1(path, template, subdir);
74 else if (S_ISLNK(st_template.st_mode)) {
75 struct strbuf lnk = STRBUF_INIT;
76 if (strbuf_readlink(&lnk, template->buf, 0) < 0)
77 die_errno(_("cannot readlink '%s'"), template->buf);
78 if (symlink(lnk.buf, path->buf))
79 die_errno(_("cannot symlink '%s' '%s'"),
83 else if (S_ISREG(st_template.st_mode)) {
84 if (copy_file(path->buf, template->buf, st_template.st_mode))
85 die_errno(_("cannot copy '%s' to '%s'"),
86 template->buf, path->buf);
89 error(_("ignoring template %s"), template->buf);
93 static void copy_templates(const char *template_dir)
95 struct strbuf path = STRBUF_INIT;
96 struct strbuf template_path = STRBUF_INIT;
98 struct repository_format template_format;
99 struct strbuf err = STRBUF_INIT;
101 char *to_free = NULL;
104 template_dir = getenv(TEMPLATE_DIR_ENVIRONMENT);
106 template_dir = init_db_template_dir;
108 template_dir = to_free = system_path(DEFAULT_GIT_TEMPLATE_DIR);
109 if (!template_dir[0]) {
114 strbuf_addstr(&template_path, template_dir);
115 strbuf_complete(&template_path, '/');
116 template_len = template_path.len;
118 dir = opendir(template_path.buf);
120 warning(_("templates not found %s"), template_dir);
124 /* Make sure that template is from the correct vintage */
125 strbuf_addstr(&template_path, "config");
126 read_repository_format(&template_format, template_path.buf);
127 strbuf_setlen(&template_path, template_len);
130 * No mention of version at all is OK, but anything else should be
133 if (template_format.version >= 0 &&
134 verify_repository_format(&template_format, &err) < 0) {
135 warning(_("not copying templates from '%s': %s"),
136 template_dir, err.buf);
137 strbuf_release(&err);
138 goto close_free_return;
141 strbuf_addstr(&path, get_git_dir());
142 strbuf_complete(&path, '/');
143 copy_templates_1(&path, &template_path, dir);
148 strbuf_release(&path);
149 strbuf_release(&template_path);
152 static int git_init_db_config(const char *k, const char *v, void *cb)
154 if (!strcmp(k, "init.templatedir"))
155 return git_config_pathname(&init_db_template_dir, k, v);
161 * If the git_dir is not directly inside the working tree, then git will not
162 * find it by default, and we need to set the worktree explicitly.
164 static int needs_work_tree_config(const char *git_dir, const char *work_tree)
166 if (!strcmp(work_tree, "/") && !strcmp(git_dir, "/.git"))
168 if (skip_prefix(git_dir, work_tree, &git_dir) &&
169 !strcmp(git_dir, "/.git"))
174 static int create_default_files(const char *template_path)
177 struct strbuf buf = STRBUF_INIT;
179 char repo_version_string[10];
185 * Create .git/refs/{heads,tags}
187 safe_create_dir(git_path_buf(&buf, "refs"), 1);
188 safe_create_dir(git_path_buf(&buf, "refs/heads"), 1);
189 safe_create_dir(git_path_buf(&buf, "refs/tags"), 1);
191 /* Just look for `init.templatedir` */
192 git_config(git_init_db_config, NULL);
194 /* First copy the templates -- we might have the default
195 * config file there, in which case we would want to read
196 * from it after installing.
198 copy_templates(template_path);
200 git_config(git_default_config, NULL);
201 is_bare_repository_cfg = init_is_bare_repository;
203 /* reading existing config may have overwrote it */
204 if (init_shared_repository != -1)
205 set_shared_repository(init_shared_repository);
208 * We would have created the above under user's umask -- under
209 * shared-repository settings, we would need to fix them up.
211 if (get_shared_repository()) {
212 adjust_shared_perm(get_git_dir());
213 adjust_shared_perm(git_path_buf(&buf, "refs"));
214 adjust_shared_perm(git_path_buf(&buf, "refs/heads"));
215 adjust_shared_perm(git_path_buf(&buf, "refs/tags"));
219 * Create the default symlink from ".git/HEAD" to the "master"
220 * branch, if it does not exist yet.
222 path = git_path_buf(&buf, "HEAD");
223 reinit = (!access(path, R_OK)
224 || readlink(path, junk, sizeof(junk)-1) != -1);
226 if (create_symref("HEAD", "refs/heads/master", NULL) < 0)
230 /* This forces creation of new config file */
231 xsnprintf(repo_version_string, sizeof(repo_version_string),
232 "%d", GIT_REPO_VERSION);
233 git_config_set("core.repositoryformatversion", repo_version_string);
235 /* Check filemode trustability */
236 path = git_path_buf(&buf, "config");
237 filemode = TEST_FILEMODE;
238 if (TEST_FILEMODE && !lstat(path, &st1)) {
240 filemode = (!chmod(path, st1.st_mode ^ S_IXUSR) &&
241 !lstat(path, &st2) &&
242 st1.st_mode != st2.st_mode &&
243 !chmod(path, st1.st_mode));
244 if (filemode && !reinit && (st1.st_mode & S_IXUSR))
247 git_config_set("core.filemode", filemode ? "true" : "false");
249 if (is_bare_repository())
250 git_config_set("core.bare", "true");
252 const char *work_tree = get_git_work_tree();
253 git_config_set("core.bare", "false");
254 /* allow template config file to override the default */
255 if (log_all_ref_updates == -1)
256 git_config_set("core.logallrefupdates", "true");
257 if (needs_work_tree_config(get_git_dir(), work_tree))
258 git_config_set("core.worktree", work_tree);
262 /* Check if symlink is supported in the work tree */
263 path = git_path_buf(&buf, "tXXXXXX");
264 if (!close(xmkstemp(path)) &&
266 !symlink("testing", path) &&
267 !lstat(path, &st1) &&
268 S_ISLNK(st1.st_mode))
269 unlink(path); /* good */
271 git_config_set("core.symlinks", "false");
273 /* Check if the filesystem is case-insensitive */
274 path = git_path_buf(&buf, "CoNfIg");
275 if (!access(path, F_OK))
276 git_config_set("core.ignorecase", "true");
277 probe_utf8_pathname_composition();
280 strbuf_release(&buf);
284 static void create_object_directory(void)
286 struct strbuf path = STRBUF_INIT;
289 strbuf_addstr(&path, get_object_directory());
292 safe_create_dir(path.buf, 1);
294 strbuf_setlen(&path, baselen);
295 strbuf_addstr(&path, "/pack");
296 safe_create_dir(path.buf, 1);
298 strbuf_setlen(&path, baselen);
299 strbuf_addstr(&path, "/info");
300 safe_create_dir(path.buf, 1);
302 strbuf_release(&path);
305 int set_git_dir_init(const char *git_dir, const char *real_git_dir,
311 if (!exist_ok && !stat(git_dir, &st))
312 die(_("%s already exists"), git_dir);
314 if (!exist_ok && !stat(real_git_dir, &st))
315 die(_("%s already exists"), real_git_dir);
318 * make sure symlinks are resolved because we'll be
319 * moving the target repo later on in separate_git_dir()
321 git_link = xstrdup(real_path(git_dir));
322 set_git_dir(real_path(real_git_dir));
325 set_git_dir(real_path(git_dir));
328 startup_info->have_repository = 1;
332 static void separate_git_dir(const char *git_dir)
336 if (!stat(git_link, &st)) {
339 if (S_ISREG(st.st_mode))
340 src = read_gitfile(git_link);
341 else if (S_ISDIR(st.st_mode))
344 die(_("unable to handle file type %d"), (int)st.st_mode);
346 if (rename(src, git_dir))
347 die_errno(_("unable to move %s to %s"), src, git_dir);
350 write_file(git_link, "gitdir: %s", git_dir);
353 int init_db(const char *template_dir, unsigned int flags)
356 const char *git_dir = get_git_dir();
359 separate_git_dir(git_dir);
361 safe_create_dir(git_dir, 0);
363 init_is_bare_repository = is_bare_repository();
365 /* Check to see if the repository version is right.
366 * Note that a newly created repository does not have
367 * config file, so this will not fail. What we are catching
368 * is an attempt to reinitialize new repository with an old tool.
370 check_repository_format();
372 reinit = create_default_files(template_dir);
374 create_object_directory();
376 if (get_shared_repository()) {
378 /* We do not spell "group" and such, so that
379 * the configuration can be read by older version
380 * of git. Note, we use octal numbers for new share modes,
381 * and compatibility values for PERM_GROUP and
384 if (get_shared_repository() < 0)
385 /* force to the mode value */
386 xsnprintf(buf, sizeof(buf), "0%o", -get_shared_repository());
387 else if (get_shared_repository() == PERM_GROUP)
388 xsnprintf(buf, sizeof(buf), "%d", OLD_PERM_GROUP);
389 else if (get_shared_repository() == PERM_EVERYBODY)
390 xsnprintf(buf, sizeof(buf), "%d", OLD_PERM_EVERYBODY);
392 die("BUG: invalid value for shared_repository");
393 git_config_set("core.sharedrepository", buf);
394 git_config_set("receive.denyNonFastforwards", "true");
397 if (!(flags & INIT_DB_QUIET)) {
398 int len = strlen(git_dir);
400 /* TRANSLATORS: The first '%s' is either "Reinitialized
401 existing" or "Initialized empty", the second " shared" or
402 "", and the last '%s%s' is the verbatim directory name. */
403 printf(_("%s%s Git repository in %s%s\n"),
404 reinit ? _("Reinitialized existing") : _("Initialized empty"),
405 get_shared_repository() ? _(" shared") : "",
406 git_dir, len && git_dir[len-1] != '/' ? "/" : "");
412 static int guess_repository_type(const char *git_dir)
419 * "GIT_DIR=. git init" is always bare.
420 * "GIT_DIR=`pwd` git init" too.
422 if (!strcmp(".", git_dir))
425 cwd_is_git_dir = !strcmp(git_dir, cwd);
430 * "GIT_DIR=.git or GIT_DIR=something/.git is usually not.
432 if (!strcmp(git_dir, ".git"))
434 slash = strrchr(git_dir, '/');
435 if (slash && !strcmp(slash, "/.git"))
439 * Otherwise it is often bare. At this point
440 * we are just guessing.
445 static int shared_callback(const struct option *opt, const char *arg, int unset)
447 *((int *) opt->value) = (arg) ? git_config_perm("arg", arg) : PERM_GROUP;
451 static const char *const init_db_usage[] = {
452 N_("git init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]] [<directory>]"),
457 * If you want to, you can share the DB area with any number of branches.
458 * That has advantages: you can save space by sharing all the SHA1 objects.
459 * On the other hand, it might just make lookup slower and messier. You
460 * be the judge. The default case is to have one DB per managed directory.
462 int cmd_init_db(int argc, const char **argv, const char *prefix)
465 const char *real_git_dir = NULL;
466 const char *work_tree;
467 const char *template_dir = NULL;
468 unsigned int flags = 0;
469 const struct option init_db_options[] = {
470 OPT_STRING(0, "template", &template_dir, N_("template-directory"),
471 N_("directory from which templates will be used")),
472 OPT_SET_INT(0, "bare", &is_bare_repository_cfg,
473 N_("create a bare repository"), 1),
474 { OPTION_CALLBACK, 0, "shared", &init_shared_repository,
476 N_("specify that the git repository is to be shared amongst several users"),
477 PARSE_OPT_OPTARG | PARSE_OPT_NONEG, shared_callback, 0},
478 OPT_BIT('q', "quiet", &flags, N_("be quiet"), INIT_DB_QUIET),
479 OPT_STRING(0, "separate-git-dir", &real_git_dir, N_("gitdir"),
480 N_("separate git dir from working tree")),
484 argc = parse_options(argc, argv, prefix, init_db_options, init_db_usage, 0);
486 if (real_git_dir && !is_absolute_path(real_git_dir))
487 real_git_dir = xstrdup(real_path(real_git_dir));
492 if (chdir(argv[0]) < 0) {
496 * At this point we haven't read any configuration,
497 * and we know shared_repository should always be 0;
498 * but just in case we play safe.
500 saved = get_shared_repository();
501 set_shared_repository(0);
502 switch (safe_create_leading_directories_const(argv[0])) {
510 die_errno(_("cannot mkdir %s"), argv[0]);
513 set_shared_repository(saved);
514 if (mkdir(argv[0], 0777) < 0)
515 die_errno(_("cannot mkdir %s"), argv[0]);
519 die_errno(_("cannot chdir to %s"), argv[0]);
521 } else if (0 < argc) {
522 usage(init_db_usage[0]);
524 if (is_bare_repository_cfg == 1) {
525 char *cwd = xgetcwd();
526 setenv(GIT_DIR_ENVIRONMENT, cwd, argc > 0);
530 if (init_shared_repository != -1)
531 set_shared_repository(init_shared_repository);
534 * GIT_WORK_TREE makes sense only in conjunction with GIT_DIR
535 * without --bare. Catch the error early.
537 git_dir = getenv(GIT_DIR_ENVIRONMENT);
538 work_tree = getenv(GIT_WORK_TREE_ENVIRONMENT);
539 if ((!git_dir || is_bare_repository_cfg == 1) && work_tree)
540 die(_("%s (or --work-tree=<directory>) not allowed without "
541 "specifying %s (or --git-dir=<directory>)"),
542 GIT_WORK_TREE_ENVIRONMENT,
543 GIT_DIR_ENVIRONMENT);
546 * Set up the default .git directory contents
549 git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
551 if (is_bare_repository_cfg < 0)
552 is_bare_repository_cfg = guess_repository_type(git_dir);
554 if (!is_bare_repository_cfg) {
555 const char *git_dir_parent = strrchr(git_dir, '/');
556 if (git_dir_parent) {
557 char *rel = xstrndup(git_dir, git_dir_parent - git_dir);
558 git_work_tree_cfg = xstrdup(real_path(rel));
561 if (!git_work_tree_cfg)
562 git_work_tree_cfg = xgetcwd();
564 set_git_work_tree(work_tree);
566 set_git_work_tree(git_work_tree_cfg);
567 if (access(get_git_work_tree(), X_OK))
568 die_errno (_("Cannot access work tree '%s'"),
569 get_git_work_tree());
573 set_git_work_tree(work_tree);
576 set_git_dir_init(git_dir, real_git_dir, 1);
578 return init_db(template_dir, flags);