Merge branch 'tb/log-G-binary'
[git.git] / builtin / init-db.c
blob41faffd28db8850fb97daa52c6d481375b380504
1 /*
2 * GIT - The information manager from hell
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"
13 #ifndef DEFAULT_GIT_TEMPLATE_DIR
14 #define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates"
15 #endif
17 #ifdef NO_TRUSTABLE_FILEMODE
18 #define TEST_FILEMODE 0
19 #else
20 #define TEST_FILEMODE 1
21 #endif
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,
28 DIR *dir)
30 size_t path_baselen = path->len;
31 size_t template_baselen = template_path->len;
32 struct dirent *de;
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;
44 int exists = 0;
46 strbuf_setlen(path, path_baselen);
47 strbuf_setlen(template_path, template_baselen);
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);
57 else
58 exists = 1;
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);
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);
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);
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);
89 else
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;
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;
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;
115 strbuf_addstr(&template_path, template_dir);
116 strbuf_complete(&template_path, '/');
117 template_len = template_path.len;
119 dir = opendir(template_path.buf);
120 if (!dir) {
121 warning(_("templates not found in %s"), template_dir);
122 goto free_return;
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
132 * verified.
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);
145 close_free_return:
146 closedir(dir);
147 free_return:
148 free(to_free);
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 return 0;
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.
165 static int needs_work_tree_config(const char *git_dir, const char *work_tree)
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;
175 static int create_default_files(const char *template_path,
176 const char *original_git_dir)
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;
187 /* Just look for `init.templatedir` */
188 git_config(git_init_db_config, NULL);
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.
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).
199 copy_templates(template_path);
200 git_config_clear();
201 reset_shared_repository();
202 git_config(git_default_config, NULL);
205 * We must make sure command-line options continue to override any
206 * values we might have just re-read from the config.
208 is_bare_repository_cfg = init_is_bare_repository;
209 if (init_shared_repository != -1)
210 set_shared_repository(init_shared_repository);
213 * We would have created the above under user's umask -- under
214 * shared-repository settings, we would need to fix them up.
216 if (get_shared_repository()) {
217 adjust_shared_perm(get_git_dir());
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.
224 safe_create_dir(git_path("refs"), 1);
225 adjust_shared_perm(git_path("refs"));
227 if (refs_init_db(&err))
228 die("failed to set up refs db: %s", err.buf);
231 * Create the default symlink from ".git/HEAD" to the "master"
232 * branch, if it does not exist yet.
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);
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);
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;
259 git_config_set("core.filemode", filemode ? "true" : "false");
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);
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");
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();
292 strbuf_release(&buf);
293 return reinit;
296 static void create_object_directory(void)
298 struct strbuf path = STRBUF_INIT;
299 size_t baselen;
301 strbuf_addstr(&path, get_object_directory());
302 baselen = path.len;
304 safe_create_dir(path.buf, 1);
306 strbuf_setlen(&path, baselen);
307 strbuf_addstr(&path, "/pack");
308 safe_create_dir(path.buf, 1);
310 strbuf_setlen(&path, baselen);
311 strbuf_addstr(&path, "/info");
312 safe_create_dir(path.buf, 1);
314 strbuf_release(&path);
317 static void separate_git_dir(const char *git_dir, const char *git_link)
319 struct stat st;
321 if (!stat(git_link, &st)) {
322 const char *src;
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);
331 if (rename(src, git_dir))
332 die_errno(_("unable to move %s to %s"), src, git_dir);
335 write_file(git_link, "gitdir: %s", git_dir);
338 int init_db(const char *git_dir, const char *real_git_dir,
339 const char *template_dir, unsigned int flags)
341 int reinit;
342 int exist_ok = flags & INIT_DB_EXIST_OK;
343 char *original_git_dir = real_pathdup(git_dir, 1);
345 if (real_git_dir) {
346 struct stat st;
348 if (!exist_ok && !stat(git_dir, &st))
349 die(_("%s already exists"), git_dir);
351 if (!exist_ok && !stat(real_git_dir, &st))
352 die(_("%s already exists"), real_git_dir);
354 set_git_dir(real_path(real_git_dir));
355 git_dir = get_git_dir();
356 separate_git_dir(git_dir, original_git_dir);
358 else {
359 set_git_dir(real_path(git_dir));
360 git_dir = get_git_dir();
362 startup_info->have_repository = 1;
364 safe_create_dir(git_dir, 0);
366 init_is_bare_repository = is_bare_repository();
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.
373 check_repository_format();
375 reinit = create_default_files(template_dir, original_git_dir);
377 create_object_directory();
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.
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");
400 if (!(flags & INIT_DB_QUIET)) {
401 int len = strlen(git_dir);
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] != '/' ? "/" : "");
415 free(original_git_dir);
416 return 0;
419 static int guess_repository_type(const char *git_dir)
421 const char *slash;
422 char *cwd;
423 int cwd_is_git_dir;
426 * "GIT_DIR=. git init" is always bare.
427 * "GIT_DIR=`pwd` git init" too.
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;
437 * "GIT_DIR=.git or GIT_DIR=something/.git is usually not.
439 if (!strcmp(git_dir, ".git"))
440 return 0;
441 slash = strrchr(git_dir, '/');
442 if (slash && !strcmp(slash, "/.git"))
443 return 0;
446 * Otherwise it is often bare. At this point
447 * we are just guessing.
449 return 1;
452 static int shared_callback(const struct option *opt, const char *arg, int unset)
454 BUG_ON_OPT_NEG(unset);
455 *((int *) opt->value) = (arg) ? git_config_perm("arg", arg) : PERM_GROUP;
456 return 0;
459 static const char *const init_db_usage[] = {
460 N_("git init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]] [<directory>]"),
461 NULL
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.
470 int cmd_init_db(int argc, const char **argv, const char *prefix)
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()
492 argc = parse_options(argc, argv, prefix, init_db_options, init_db_usage, 0);
494 if (real_git_dir && !is_absolute_path(real_git_dir))
495 real_git_dir = real_pathdup(real_git_dir, 1);
497 if (argc == 1) {
498 int mkdir_tried = 0;
499 retry:
500 if (chdir(argv[0]) < 0) {
501 if (!mkdir_tried) {
502 int saved;
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.
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;
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;
527 die_errno(_("cannot chdir to %s"), argv[0]);
529 } else if (0 < argc) {
530 usage(init_db_usage[0]);
532 if (is_bare_repository_cfg == 1) {
533 char *cwd = xgetcwd();
534 setenv(GIT_DIR_ENVIRONMENT, cwd, argc > 0);
535 free(cwd);
538 if (init_shared_repository != -1)
539 set_shared_repository(init_shared_repository);
542 * GIT_WORK_TREE makes sense only in conjunction with GIT_DIR
543 * without --bare. Catch the error early.
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);
554 * Set up the default .git directory contents
556 if (!git_dir)
557 git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
559 if (is_bare_repository_cfg < 0)
560 is_bare_repository_cfg = guess_repository_type(git_dir);
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);
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());
579 else {
580 if (work_tree)
581 set_git_work_tree(work_tree);
584 UNLEAK(real_git_dir);
586 flags |= INIT_DB_EXIST_OK;
587 return init_db(git_dir, real_git_dir, template_dir, flags);