Skip tests that fail due to incomplete implementations, missing tools...
[git/mingw/j6t.git] / builtin / init-db.c
blobf59f40768e90cae98cf6541f4c383d762b68b9d1
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "cache.h"
7 #include "refs.h"
8 #include "builtin.h"
9 #include "exec_cmd.h"
10 #include "parse-options.h"
12 #ifndef DEFAULT_GIT_TEMPLATE_DIR
13 #define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates"
14 #endif
16 #ifdef NO_TRUSTABLE_FILEMODE
17 #define TEST_FILEMODE 0
18 #else
19 #define TEST_FILEMODE 1
20 #endif
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 safe_create_dir(const char *dir, int share)
29 if (mkdir(dir, 0777) < 0) {
30 if (errno != EEXIST) {
31 perror(dir);
32 exit(1);
35 else if (share && adjust_shared_perm(dir))
36 die(_("Could not make %s writable by group"), dir);
39 static void copy_templates_1(struct strbuf *path, struct strbuf *template,
40 DIR *dir)
42 size_t path_baselen = path->len;
43 size_t template_baselen = template->len;
44 struct dirent *de;
46 /* Note: if ".git/hooks" file exists in the repository being
47 * re-initialized, /etc/core-git/templates/hooks/update would
48 * cause "git init" to fail here. I think this is sane but
49 * it means that the set of templates we ship by default, along
50 * with the way the namespace under .git/ is organized, should
51 * be really carefully chosen.
53 safe_create_dir(path->buf, 1);
54 while ((de = readdir(dir)) != NULL) {
55 struct stat st_git, st_template;
56 int exists = 0;
58 strbuf_setlen(path, path_baselen);
59 strbuf_setlen(template, template_baselen);
61 if (de->d_name[0] == '.')
62 continue;
63 strbuf_addstr(path, de->d_name);
64 strbuf_addstr(template, de->d_name);
65 if (lstat(path->buf, &st_git)) {
66 if (errno != ENOENT)
67 die_errno(_("cannot stat '%s'"), path->buf);
69 else
70 exists = 1;
72 if (lstat(template->buf, &st_template))
73 die_errno(_("cannot stat template '%s'"), template->buf);
75 if (S_ISDIR(st_template.st_mode)) {
76 DIR *subdir = opendir(template->buf);
77 if (!subdir)
78 die_errno(_("cannot opendir '%s'"), template->buf);
79 strbuf_addch(path, '/');
80 strbuf_addch(template, '/');
81 copy_templates_1(path, template, subdir);
82 closedir(subdir);
84 else if (exists)
85 continue;
86 else if (S_ISLNK(st_template.st_mode)) {
87 struct strbuf lnk = STRBUF_INIT;
88 if (strbuf_readlink(&lnk, template->buf, 0) < 0)
89 die_errno(_("cannot readlink '%s'"), template->buf);
90 if (symlink(lnk.buf, path->buf))
91 die_errno(_("cannot symlink '%s' '%s'"),
92 lnk.buf, path->buf);
93 strbuf_release(&lnk);
95 else if (S_ISREG(st_template.st_mode)) {
96 if (copy_file(path->buf, template->buf, st_template.st_mode))
97 die_errno(_("cannot copy '%s' to '%s'"),
98 template->buf, path->buf);
100 else
101 error(_("ignoring template %s"), template->buf);
105 static void copy_templates(const char *template_dir)
107 struct strbuf path = STRBUF_INIT;
108 struct strbuf template_path = STRBUF_INIT;
109 size_t template_len;
110 DIR *dir;
111 char *to_free = NULL;
113 if (!template_dir)
114 template_dir = getenv(TEMPLATE_DIR_ENVIRONMENT);
115 if (!template_dir)
116 template_dir = init_db_template_dir;
117 if (!template_dir)
118 template_dir = to_free = system_path(DEFAULT_GIT_TEMPLATE_DIR);
119 if (!template_dir[0]) {
120 free(to_free);
121 return;
124 strbuf_addstr(&template_path, template_dir);
125 strbuf_complete(&template_path, '/');
126 template_len = template_path.len;
128 dir = opendir(template_path.buf);
129 if (!dir) {
130 warning(_("templates not found %s"), template_dir);
131 goto free_return;
134 /* Make sure that template is from the correct vintage */
135 strbuf_addstr(&template_path, "config");
136 repository_format_version = 0;
137 git_config_from_file(check_repository_format_version,
138 template_path.buf, NULL);
139 strbuf_setlen(&template_path, template_len);
141 if (repository_format_version &&
142 repository_format_version != GIT_REPO_VERSION) {
143 warning(_("not copying templates of "
144 "a wrong format version %d from '%s'"),
145 repository_format_version,
146 template_dir);
147 goto close_free_return;
150 strbuf_addstr(&path, get_git_dir());
151 strbuf_complete(&path, '/');
152 copy_templates_1(&path, &template_path, dir);
153 close_free_return:
154 closedir(dir);
155 free_return:
156 free(to_free);
157 strbuf_release(&path);
158 strbuf_release(&template_path);
161 static int git_init_db_config(const char *k, const char *v, void *cb)
163 if (!strcmp(k, "init.templatedir"))
164 return git_config_pathname(&init_db_template_dir, k, v);
166 return 0;
170 * If the git_dir is not directly inside the working tree, then git will not
171 * find it by default, and we need to set the worktree explicitly.
173 static int needs_work_tree_config(const char *git_dir, const char *work_tree)
175 if (!strcmp(work_tree, "/") && !strcmp(git_dir, "/.git"))
176 return 0;
177 if (skip_prefix(git_dir, work_tree, &git_dir) &&
178 !strcmp(git_dir, "/.git"))
179 return 0;
180 return 1;
183 static int create_default_files(const char *template_path)
185 struct stat st1;
186 struct strbuf buf = STRBUF_INIT;
187 char *path;
188 char repo_version_string[10];
189 char junk[2];
190 int reinit;
191 int filemode;
194 * Create .git/refs/{heads,tags}
196 safe_create_dir(git_path_buf(&buf, "refs"), 1);
197 safe_create_dir(git_path_buf(&buf, "refs/heads"), 1);
198 safe_create_dir(git_path_buf(&buf, "refs/tags"), 1);
200 /* Just look for `init.templatedir` */
201 git_config(git_init_db_config, NULL);
203 /* First copy the templates -- we might have the default
204 * config file there, in which case we would want to read
205 * from it after installing.
207 copy_templates(template_path);
209 git_config(git_default_config, NULL);
210 is_bare_repository_cfg = init_is_bare_repository;
212 /* reading existing config may have overwrote it */
213 if (init_shared_repository != -1)
214 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 (shared_repository) {
221 adjust_shared_perm(get_git_dir());
222 adjust_shared_perm(git_path_buf(&buf, "refs"));
223 adjust_shared_perm(git_path_buf(&buf, "refs/heads"));
224 adjust_shared_perm(git_path_buf(&buf, "refs/tags"));
228 * Create the default symlink from ".git/HEAD" to the "master"
229 * branch, if it does not exist yet.
231 path = git_path_buf(&buf, "HEAD");
232 reinit = (!access(path, R_OK)
233 || readlink(path, junk, sizeof(junk)-1) != -1);
234 if (!reinit) {
235 if (create_symref("HEAD", "refs/heads/master", NULL) < 0)
236 exit(1);
239 /* This forces creation of new config file */
240 xsnprintf(repo_version_string, sizeof(repo_version_string),
241 "%d", GIT_REPO_VERSION);
242 git_config_set("core.repositoryformatversion", repo_version_string);
244 /* Check filemode trustability */
245 path = git_path_buf(&buf, "config");
246 filemode = TEST_FILEMODE;
247 if (TEST_FILEMODE && !lstat(path, &st1)) {
248 struct stat st2;
249 filemode = (!chmod(path, st1.st_mode ^ S_IXUSR) &&
250 !lstat(path, &st2) &&
251 st1.st_mode != st2.st_mode &&
252 !chmod(path, st1.st_mode));
253 if (filemode && !reinit && (st1.st_mode & S_IXUSR))
254 filemode = 0;
256 git_config_set("core.filemode", filemode ? "true" : "false");
258 if (is_bare_repository())
259 git_config_set("core.bare", "true");
260 else {
261 const char *work_tree = get_git_work_tree();
262 git_config_set("core.bare", "false");
263 /* allow template config file to override the default */
264 if (log_all_ref_updates == -1)
265 git_config_set("core.logallrefupdates", "true");
266 if (needs_work_tree_config(get_git_dir(), work_tree))
267 git_config_set("core.worktree", work_tree);
270 if (!reinit) {
271 /* Check if symlink is supported in the work tree */
272 path = git_path_buf(&buf, "tXXXXXX");
273 if (!close(xmkstemp(path)) &&
274 !unlink(path) &&
275 !symlink("testing", path) &&
276 !lstat(path, &st1) &&
277 S_ISLNK(st1.st_mode))
278 unlink(path); /* good */
279 else
280 git_config_set("core.symlinks", "false");
282 /* Check if the filesystem is case-insensitive */
283 path = git_path_buf(&buf, "CoNfIg");
284 if (!access(path, F_OK))
285 git_config_set("core.ignorecase", "true");
286 probe_utf8_pathname_composition();
289 strbuf_release(&buf);
290 return reinit;
293 static void create_object_directory(void)
295 struct strbuf path = STRBUF_INIT;
296 size_t baselen;
298 strbuf_addstr(&path, get_object_directory());
299 baselen = path.len;
301 safe_create_dir(path.buf, 1);
303 strbuf_setlen(&path, baselen);
304 strbuf_addstr(&path, "/pack");
305 safe_create_dir(path.buf, 1);
307 strbuf_setlen(&path, baselen);
308 strbuf_addstr(&path, "/info");
309 safe_create_dir(path.buf, 1);
311 strbuf_release(&path);
314 int set_git_dir_init(const char *git_dir, const char *real_git_dir,
315 int exist_ok)
317 if (real_git_dir) {
318 struct stat st;
320 if (!exist_ok && !stat(git_dir, &st))
321 die(_("%s already exists"), git_dir);
323 if (!exist_ok && !stat(real_git_dir, &st))
324 die(_("%s already exists"), real_git_dir);
327 * make sure symlinks are resolved because we'll be
328 * moving the target repo later on in separate_git_dir()
330 git_link = xstrdup(real_path(git_dir));
331 set_git_dir(real_path(real_git_dir));
333 else {
334 set_git_dir(real_path(git_dir));
335 git_link = NULL;
337 return 0;
340 static void separate_git_dir(const char *git_dir)
342 struct stat st;
344 if (!stat(git_link, &st)) {
345 const char *src;
347 if (S_ISREG(st.st_mode))
348 src = read_gitfile(git_link);
349 else if (S_ISDIR(st.st_mode))
350 src = git_link;
351 else
352 die(_("unable to handle file type %d"), (int)st.st_mode);
354 if (rename(src, git_dir))
355 die_errno(_("unable to move %s to %s"), src, git_dir);
358 write_file(git_link, "gitdir: %s", git_dir);
361 int init_db(const char *template_dir, unsigned int flags)
363 int reinit;
364 const char *git_dir = get_git_dir();
366 if (git_link)
367 separate_git_dir(git_dir);
369 safe_create_dir(git_dir, 0);
371 init_is_bare_repository = is_bare_repository();
373 /* Check to see if the repository version is right.
374 * Note that a newly created repository does not have
375 * config file, so this will not fail. What we are catching
376 * is an attempt to reinitialize new repository with an old tool.
378 check_repository_format();
380 reinit = create_default_files(template_dir);
382 create_object_directory();
384 if (shared_repository) {
385 char buf[10];
386 /* We do not spell "group" and such, so that
387 * the configuration can be read by older version
388 * of git. Note, we use octal numbers for new share modes,
389 * and compatibility values for PERM_GROUP and
390 * PERM_EVERYBODY.
392 if (shared_repository < 0)
393 /* force to the mode value */
394 xsnprintf(buf, sizeof(buf), "0%o", -shared_repository);
395 else if (shared_repository == PERM_GROUP)
396 xsnprintf(buf, sizeof(buf), "%d", OLD_PERM_GROUP);
397 else if (shared_repository == PERM_EVERYBODY)
398 xsnprintf(buf, sizeof(buf), "%d", OLD_PERM_EVERYBODY);
399 else
400 die("BUG: invalid value for shared_repository");
401 git_config_set("core.sharedrepository", buf);
402 git_config_set("receive.denyNonFastforwards", "true");
405 if (!(flags & INIT_DB_QUIET)) {
406 int len = strlen(git_dir);
408 /* TRANSLATORS: The first '%s' is either "Reinitialized
409 existing" or "Initialized empty", the second " shared" or
410 "", and the last '%s%s' is the verbatim directory name. */
411 printf(_("%s%s Git repository in %s%s\n"),
412 reinit ? _("Reinitialized existing") : _("Initialized empty"),
413 shared_repository ? _(" shared") : "",
414 git_dir, len && git_dir[len-1] != '/' ? "/" : "");
417 return 0;
420 static int guess_repository_type(const char *git_dir)
422 const char *slash;
423 char *cwd;
424 int cwd_is_git_dir;
427 * "GIT_DIR=. git init" is always bare.
428 * "GIT_DIR=`pwd` git init" too.
430 if (!strcmp(".", git_dir))
431 return 1;
432 cwd = xgetcwd();
433 cwd_is_git_dir = !strcmp(git_dir, cwd);
434 free(cwd);
435 if (cwd_is_git_dir)
436 return 1;
438 * "GIT_DIR=.git or GIT_DIR=something/.git is usually not.
440 if (!strcmp(git_dir, ".git"))
441 return 0;
442 slash = strrchr(git_dir, '/');
443 if (slash && !strcmp(slash, "/.git"))
444 return 0;
447 * Otherwise it is often bare. At this point
448 * we are just guessing.
450 return 1;
453 static int shared_callback(const struct option *opt, const char *arg, int 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 = xstrdup(real_path(real_git_dir));
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 = shared_repository;
509 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 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 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 = xstrdup(real_path(rel));
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 set_git_dir_init(git_dir, real_git_dir, 1);
586 return init_db(template_dir, flags);