cache: update object ID functions for the_hash_algo
[git.git] / builtin / init-db.c
blob4ecf90936822e64998651e4d51d1be9c921dee60
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, 0) < 0)
77 die_errno(_("cannot readlink '%s'"), template_path->buf);
78 if (symlink(lnk.buf, path->buf))
79 die_errno(_("cannot symlink '%s' '%s'"),
80 lnk.buf, path->buf);
81 strbuf_release(&lnk);
83 else if (S_ISREG(st_template.st_mode)) {
84 if (copy_file(path->buf, template_path->buf, st_template.st_mode))
85 die_errno(_("cannot copy '%s' to '%s'"),
86 template_path->buf, path->buf);
88 else
89 error(_("ignoring template %s"), template_path->buf);
93 static void copy_templates(const char *template_dir)
95 struct strbuf path = STRBUF_INIT;
96 struct strbuf template_path = STRBUF_INIT;
97 size_t template_len;
98 struct repository_format template_format;
99 struct strbuf err = STRBUF_INIT;
100 DIR *dir;
101 char *to_free = NULL;
103 if (!template_dir)
104 template_dir = getenv(TEMPLATE_DIR_ENVIRONMENT);
105 if (!template_dir)
106 template_dir = init_db_template_dir;
107 if (!template_dir)
108 template_dir = to_free = system_path(DEFAULT_GIT_TEMPLATE_DIR);
109 if (!template_dir[0]) {
110 free(to_free);
111 return;
114 strbuf_addstr(&template_path, template_dir);
115 strbuf_complete(&template_path, '/');
116 template_len = template_path.len;
118 dir = opendir(template_path.buf);
119 if (!dir) {
120 warning(_("templates not found in %s"), template_dir);
121 goto free_return;
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
131 * verified.
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_common_dir());
142 strbuf_complete(&path, '/');
143 copy_templates_1(&path, &template_path, dir);
144 close_free_return:
145 closedir(dir);
146 free_return:
147 free(to_free);
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);
157 return 0;
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"))
167 return 0;
168 if (skip_prefix(git_dir, work_tree, &git_dir) &&
169 !strcmp(git_dir, "/.git"))
170 return 0;
171 return 1;
174 static int create_default_files(const char *template_path,
175 const char *original_git_dir)
177 struct stat st1;
178 struct strbuf buf = STRBUF_INIT;
179 char *path;
180 char repo_version_string[10];
181 char junk[2];
182 int reinit;
183 int filemode;
184 struct strbuf err = STRBUF_INIT;
186 /* Just look for `init.templatedir` */
187 git_config(git_init_db_config, NULL);
190 * First copy the templates -- we might have the default
191 * config file there, in which case we would want to read
192 * from it after installing.
194 * Before reading that config, we also need to clear out any cached
195 * values (since we've just potentially changed what's available on
196 * disk).
198 copy_templates(template_path);
199 git_config_clear();
200 reset_shared_repository();
201 git_config(git_default_config, NULL);
204 * We must make sure command-line options continue to override any
205 * values we might have just re-read from the config.
207 is_bare_repository_cfg = init_is_bare_repository;
208 if (init_shared_repository != -1)
209 set_shared_repository(init_shared_repository);
212 * We would have created the above under user's umask -- under
213 * shared-repository settings, we would need to fix them up.
215 if (get_shared_repository()) {
216 adjust_shared_perm(get_git_dir());
220 * We need to create a "refs" dir in any case so that older
221 * versions of git can tell that this is a repository.
223 safe_create_dir(git_path("refs"), 1);
224 adjust_shared_perm(git_path("refs"));
226 if (refs_init_db(&err))
227 die("failed to set up refs db: %s", err.buf);
230 * Create the default symlink from ".git/HEAD" to the "master"
231 * branch, if it does not exist yet.
233 path = git_path_buf(&buf, "HEAD");
234 reinit = (!access(path, R_OK)
235 || readlink(path, junk, sizeof(junk)-1) != -1);
236 if (!reinit) {
237 if (create_symref("HEAD", "refs/heads/master", NULL) < 0)
238 exit(1);
241 /* This forces creation of new config file */
242 xsnprintf(repo_version_string, sizeof(repo_version_string),
243 "%d", GIT_REPO_VERSION);
244 git_config_set("core.repositoryformatversion", repo_version_string);
246 /* Check filemode trustability */
247 path = git_path_buf(&buf, "config");
248 filemode = TEST_FILEMODE;
249 if (TEST_FILEMODE && !lstat(path, &st1)) {
250 struct stat st2;
251 filemode = (!chmod(path, st1.st_mode ^ S_IXUSR) &&
252 !lstat(path, &st2) &&
253 st1.st_mode != st2.st_mode &&
254 !chmod(path, st1.st_mode));
255 if (filemode && !reinit && (st1.st_mode & S_IXUSR))
256 filemode = 0;
258 git_config_set("core.filemode", filemode ? "true" : "false");
260 if (is_bare_repository())
261 git_config_set("core.bare", "true");
262 else {
263 const char *work_tree = get_git_work_tree();
264 git_config_set("core.bare", "false");
265 /* allow template config file to override the default */
266 if (log_all_ref_updates == LOG_REFS_UNSET)
267 git_config_set("core.logallrefupdates", "true");
268 if (needs_work_tree_config(original_git_dir, work_tree))
269 git_config_set("core.worktree", work_tree);
272 if (!reinit) {
273 /* Check if symlink is supported in the work tree */
274 path = git_path_buf(&buf, "tXXXXXX");
275 if (!close(xmkstemp(path)) &&
276 !unlink(path) &&
277 !symlink("testing", path) &&
278 !lstat(path, &st1) &&
279 S_ISLNK(st1.st_mode))
280 unlink(path); /* good */
281 else
282 git_config_set("core.symlinks", "false");
284 /* Check if the filesystem is case-insensitive */
285 path = git_path_buf(&buf, "CoNfIg");
286 if (!access(path, F_OK))
287 git_config_set("core.ignorecase", "true");
288 probe_utf8_pathname_composition();
291 strbuf_release(&buf);
292 return reinit;
295 static void create_object_directory(void)
297 struct strbuf path = STRBUF_INIT;
298 size_t baselen;
300 strbuf_addstr(&path, get_object_directory());
301 baselen = path.len;
303 safe_create_dir(path.buf, 1);
305 strbuf_setlen(&path, baselen);
306 strbuf_addstr(&path, "/pack");
307 safe_create_dir(path.buf, 1);
309 strbuf_setlen(&path, baselen);
310 strbuf_addstr(&path, "/info");
311 safe_create_dir(path.buf, 1);
313 strbuf_release(&path);
316 static void separate_git_dir(const char *git_dir, const char *git_link)
318 struct stat st;
320 if (!stat(git_link, &st)) {
321 const char *src;
323 if (S_ISREG(st.st_mode))
324 src = read_gitfile(git_link);
325 else if (S_ISDIR(st.st_mode))
326 src = git_link;
327 else
328 die(_("unable to handle file type %d"), (int)st.st_mode);
330 if (rename(src, git_dir))
331 die_errno(_("unable to move %s to %s"), src, git_dir);
334 write_file(git_link, "gitdir: %s", git_dir);
337 int init_db(const char *git_dir, const char *real_git_dir,
338 const char *template_dir, unsigned int flags)
340 int reinit;
341 int exist_ok = flags & INIT_DB_EXIST_OK;
342 char *original_git_dir = real_pathdup(git_dir, 1);
344 if (real_git_dir) {
345 struct stat st;
347 if (!exist_ok && !stat(git_dir, &st))
348 die(_("%s already exists"), git_dir);
350 if (!exist_ok && !stat(real_git_dir, &st))
351 die(_("%s already exists"), real_git_dir);
353 set_git_dir(real_path(real_git_dir));
354 git_dir = get_git_dir();
355 separate_git_dir(git_dir, original_git_dir);
357 else {
358 set_git_dir(real_path(git_dir));
359 git_dir = get_git_dir();
361 startup_info->have_repository = 1;
363 safe_create_dir(git_dir, 0);
365 init_is_bare_repository = is_bare_repository();
367 /* Check to see if the repository version is right.
368 * Note that a newly created repository does not have
369 * config file, so this will not fail. What we are catching
370 * is an attempt to reinitialize new repository with an old tool.
372 check_repository_format();
374 reinit = create_default_files(template_dir, original_git_dir);
376 create_object_directory();
378 if (get_shared_repository()) {
379 char buf[10];
380 /* We do not spell "group" and such, so that
381 * the configuration can be read by older version
382 * of git. Note, we use octal numbers for new share modes,
383 * and compatibility values for PERM_GROUP and
384 * PERM_EVERYBODY.
386 if (get_shared_repository() < 0)
387 /* force to the mode value */
388 xsnprintf(buf, sizeof(buf), "0%o", -get_shared_repository());
389 else if (get_shared_repository() == PERM_GROUP)
390 xsnprintf(buf, sizeof(buf), "%d", OLD_PERM_GROUP);
391 else if (get_shared_repository() == PERM_EVERYBODY)
392 xsnprintf(buf, sizeof(buf), "%d", OLD_PERM_EVERYBODY);
393 else
394 BUG("invalid value for shared_repository");
395 git_config_set("core.sharedrepository", buf);
396 git_config_set("receive.denyNonFastforwards", "true");
399 if (!(flags & INIT_DB_QUIET)) {
400 int len = strlen(git_dir);
402 if (reinit)
403 printf(get_shared_repository()
404 ? _("Reinitialized existing shared Git repository in %s%s\n")
405 : _("Reinitialized existing Git repository in %s%s\n"),
406 git_dir, len && git_dir[len-1] != '/' ? "/" : "");
407 else
408 printf(get_shared_repository()
409 ? _("Initialized empty shared Git repository in %s%s\n")
410 : _("Initialized empty Git repository in %s%s\n"),
411 git_dir, len && git_dir[len-1] != '/' ? "/" : "");
414 free(original_git_dir);
415 return 0;
418 static int guess_repository_type(const char *git_dir)
420 const char *slash;
421 char *cwd;
422 int cwd_is_git_dir;
425 * "GIT_DIR=. git init" is always bare.
426 * "GIT_DIR=`pwd` git init" too.
428 if (!strcmp(".", git_dir))
429 return 1;
430 cwd = xgetcwd();
431 cwd_is_git_dir = !strcmp(git_dir, cwd);
432 free(cwd);
433 if (cwd_is_git_dir)
434 return 1;
436 * "GIT_DIR=.git or GIT_DIR=something/.git is usually not.
438 if (!strcmp(git_dir, ".git"))
439 return 0;
440 slash = strrchr(git_dir, '/');
441 if (slash && !strcmp(slash, "/.git"))
442 return 0;
445 * Otherwise it is often bare. At this point
446 * we are just guessing.
448 return 1;
451 static int shared_callback(const struct option *opt, const char *arg, int unset)
453 *((int *) opt->value) = (arg) ? git_config_perm("arg", arg) : PERM_GROUP;
454 return 0;
457 static const char *const init_db_usage[] = {
458 N_("git init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]] [<directory>]"),
459 NULL
463 * If you want to, you can share the DB area with any number of branches.
464 * That has advantages: you can save space by sharing all the SHA1 objects.
465 * On the other hand, it might just make lookup slower and messier. You
466 * be the judge. The default case is to have one DB per managed directory.
468 int cmd_init_db(int argc, const char **argv, const char *prefix)
470 const char *git_dir;
471 const char *real_git_dir = NULL;
472 const char *work_tree;
473 const char *template_dir = NULL;
474 unsigned int flags = 0;
475 const struct option init_db_options[] = {
476 OPT_STRING(0, "template", &template_dir, N_("template-directory"),
477 N_("directory from which templates will be used")),
478 OPT_SET_INT(0, "bare", &is_bare_repository_cfg,
479 N_("create a bare repository"), 1),
480 { OPTION_CALLBACK, 0, "shared", &init_shared_repository,
481 N_("permissions"),
482 N_("specify that the git repository is to be shared amongst several users"),
483 PARSE_OPT_OPTARG | PARSE_OPT_NONEG, shared_callback, 0},
484 OPT_BIT('q', "quiet", &flags, N_("be quiet"), INIT_DB_QUIET),
485 OPT_STRING(0, "separate-git-dir", &real_git_dir, N_("gitdir"),
486 N_("separate git dir from working tree")),
487 OPT_END()
490 argc = parse_options(argc, argv, prefix, init_db_options, init_db_usage, 0);
492 if (real_git_dir && !is_absolute_path(real_git_dir))
493 real_git_dir = real_pathdup(real_git_dir, 1);
495 if (argc == 1) {
496 int mkdir_tried = 0;
497 retry:
498 if (chdir(argv[0]) < 0) {
499 if (!mkdir_tried) {
500 int saved;
502 * At this point we haven't read any configuration,
503 * and we know shared_repository should always be 0;
504 * but just in case we play safe.
506 saved = get_shared_repository();
507 set_shared_repository(0);
508 switch (safe_create_leading_directories_const(argv[0])) {
509 case SCLD_OK:
510 case SCLD_PERMS:
511 break;
512 case SCLD_EXISTS:
513 errno = EEXIST;
514 /* fallthru */
515 default:
516 die_errno(_("cannot mkdir %s"), argv[0]);
517 break;
519 set_shared_repository(saved);
520 if (mkdir(argv[0], 0777) < 0)
521 die_errno(_("cannot mkdir %s"), argv[0]);
522 mkdir_tried = 1;
523 goto retry;
525 die_errno(_("cannot chdir to %s"), argv[0]);
527 } else if (0 < argc) {
528 usage(init_db_usage[0]);
530 if (is_bare_repository_cfg == 1) {
531 char *cwd = xgetcwd();
532 setenv(GIT_DIR_ENVIRONMENT, cwd, argc > 0);
533 free(cwd);
536 if (init_shared_repository != -1)
537 set_shared_repository(init_shared_repository);
540 * GIT_WORK_TREE makes sense only in conjunction with GIT_DIR
541 * without --bare. Catch the error early.
543 git_dir = getenv(GIT_DIR_ENVIRONMENT);
544 work_tree = getenv(GIT_WORK_TREE_ENVIRONMENT);
545 if ((!git_dir || is_bare_repository_cfg == 1) && work_tree)
546 die(_("%s (or --work-tree=<directory>) not allowed without "
547 "specifying %s (or --git-dir=<directory>)"),
548 GIT_WORK_TREE_ENVIRONMENT,
549 GIT_DIR_ENVIRONMENT);
552 * Set up the default .git directory contents
554 if (!git_dir)
555 git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
557 if (is_bare_repository_cfg < 0)
558 is_bare_repository_cfg = guess_repository_type(git_dir);
560 if (!is_bare_repository_cfg) {
561 const char *git_dir_parent = strrchr(git_dir, '/');
562 if (git_dir_parent) {
563 char *rel = xstrndup(git_dir, git_dir_parent - git_dir);
564 git_work_tree_cfg = real_pathdup(rel, 1);
565 free(rel);
567 if (!git_work_tree_cfg)
568 git_work_tree_cfg = xgetcwd();
569 if (work_tree)
570 set_git_work_tree(work_tree);
571 else
572 set_git_work_tree(git_work_tree_cfg);
573 if (access(get_git_work_tree(), X_OK))
574 die_errno (_("Cannot access work tree '%s'"),
575 get_git_work_tree());
577 else {
578 if (work_tree)
579 set_git_work_tree(work_tree);
582 UNLEAK(real_git_dir);
584 flags |= INIT_DB_EXIST_OK;
585 return init_db(git_dir, real_git_dir, template_dir, flags);