Merge branch 'jk/pretty-encoding-doc' into maint
[git/mingw.git] / builtin / init-db.c
blobab9f86b8890ed99547144096ff56c3cbef780505
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "cache.h"
7 #include "builtin.h"
8 #include "exec_cmd.h"
9 #include "parse-options.h"
11 #ifndef DEFAULT_GIT_TEMPLATE_DIR
12 #define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates"
13 #endif
15 #ifdef NO_TRUSTABLE_FILEMODE
16 #define TEST_FILEMODE 0
17 #else
18 #define TEST_FILEMODE 1
19 #endif
21 static int init_is_bare_repository = 0;
22 static int init_shared_repository = -1;
23 static const char *init_db_template_dir;
24 static const char *git_link;
26 static void safe_create_dir(const char *dir, int share)
28 if (mkdir(dir, 0777) < 0) {
29 if (errno != EEXIST) {
30 perror(dir);
31 exit(1);
34 else if (share && adjust_shared_perm(dir))
35 die(_("Could not make %s writable by group"), dir);
38 static void copy_templates_1(char *path, int baselen,
39 char *template, int template_baselen,
40 DIR *dir)
42 struct dirent *de;
44 /* Note: if ".git/hooks" file exists in the repository being
45 * re-initialized, /etc/core-git/templates/hooks/update would
46 * cause "git init" to fail here. I think this is sane but
47 * it means that the set of templates we ship by default, along
48 * with the way the namespace under .git/ is organized, should
49 * be really carefully chosen.
51 safe_create_dir(path, 1);
52 while ((de = readdir(dir)) != NULL) {
53 struct stat st_git, st_template;
54 int namelen;
55 int exists = 0;
57 if (de->d_name[0] == '.')
58 continue;
59 namelen = strlen(de->d_name);
60 if ((PATH_MAX <= baselen + namelen) ||
61 (PATH_MAX <= template_baselen + namelen))
62 die(_("insanely long template name %s"), de->d_name);
63 memcpy(path + baselen, de->d_name, namelen+1);
64 memcpy(template + template_baselen, de->d_name, namelen+1);
65 if (lstat(path, &st_git)) {
66 if (errno != ENOENT)
67 die_errno(_("cannot stat '%s'"), path);
69 else
70 exists = 1;
72 if (lstat(template, &st_template))
73 die_errno(_("cannot stat template '%s'"), template);
75 if (S_ISDIR(st_template.st_mode)) {
76 DIR *subdir = opendir(template);
77 int baselen_sub = baselen + namelen;
78 int template_baselen_sub = template_baselen + namelen;
79 if (!subdir)
80 die_errno(_("cannot opendir '%s'"), template);
81 path[baselen_sub++] =
82 template[template_baselen_sub++] = '/';
83 path[baselen_sub] =
84 template[template_baselen_sub] = 0;
85 copy_templates_1(path, baselen_sub,
86 template, template_baselen_sub,
87 subdir);
88 closedir(subdir);
90 else if (exists)
91 continue;
92 else if (S_ISLNK(st_template.st_mode)) {
93 char lnk[256];
94 int len;
95 len = readlink(template, lnk, sizeof(lnk));
96 if (len < 0)
97 die_errno(_("cannot readlink '%s'"), template);
98 if (sizeof(lnk) <= len)
99 die(_("insanely long symlink %s"), template);
100 lnk[len] = 0;
101 if (symlink(lnk, path))
102 die_errno(_("cannot symlink '%s' '%s'"), lnk, path);
104 else if (S_ISREG(st_template.st_mode)) {
105 if (copy_file(path, template, st_template.st_mode))
106 die_errno(_("cannot copy '%s' to '%s'"), template,
107 path);
109 else
110 error(_("ignoring template %s"), template);
114 static void copy_templates(const char *template_dir)
116 char path[PATH_MAX];
117 char template_path[PATH_MAX];
118 int template_len;
119 DIR *dir;
120 const char *git_dir = get_git_dir();
121 int len = strlen(git_dir);
122 char *to_free = NULL;
124 if (!template_dir)
125 template_dir = getenv(TEMPLATE_DIR_ENVIRONMENT);
126 if (!template_dir)
127 template_dir = init_db_template_dir;
128 if (!template_dir)
129 template_dir = to_free = system_path(DEFAULT_GIT_TEMPLATE_DIR);
130 if (!template_dir[0]) {
131 free(to_free);
132 return;
134 template_len = strlen(template_dir);
135 if (PATH_MAX <= (template_len+strlen("/config")))
136 die(_("insanely long template path %s"), template_dir);
137 strcpy(template_path, template_dir);
138 if (template_path[template_len-1] != '/') {
139 template_path[template_len++] = '/';
140 template_path[template_len] = 0;
142 dir = opendir(template_path);
143 if (!dir) {
144 warning(_("templates not found %s"), template_dir);
145 goto free_return;
148 /* Make sure that template is from the correct vintage */
149 strcpy(template_path + template_len, "config");
150 repository_format_version = 0;
151 git_config_from_file(check_repository_format_version,
152 template_path, NULL);
153 template_path[template_len] = 0;
155 if (repository_format_version &&
156 repository_format_version != GIT_REPO_VERSION) {
157 warning(_("not copying templates of "
158 "a wrong format version %d from '%s'"),
159 repository_format_version,
160 template_dir);
161 goto close_free_return;
164 memcpy(path, git_dir, len);
165 if (len && path[len - 1] != '/')
166 path[len++] = '/';
167 path[len] = 0;
168 copy_templates_1(path, len,
169 template_path, template_len,
170 dir);
171 close_free_return:
172 closedir(dir);
173 free_return:
174 free(to_free);
177 static int git_init_db_config(const char *k, const char *v, void *cb)
179 if (!strcmp(k, "init.templatedir"))
180 return git_config_pathname(&init_db_template_dir, k, v);
182 return 0;
186 * If the git_dir is not directly inside the working tree, then git will not
187 * find it by default, and we need to set the worktree explicitly.
189 static int needs_work_tree_config(const char *git_dir, const char *work_tree)
191 if (!strcmp(work_tree, "/") && !strcmp(git_dir, "/.git"))
192 return 0;
193 if (skip_prefix(git_dir, work_tree, &git_dir) &&
194 !strcmp(git_dir, "/.git"))
195 return 0;
196 return 1;
199 static int create_default_files(const char *template_path)
201 const char *git_dir = get_git_dir();
202 unsigned len = strlen(git_dir);
203 static char path[PATH_MAX];
204 struct stat st1;
205 char repo_version_string[10];
206 char junk[2];
207 int reinit;
208 int filemode;
210 if (len > sizeof(path)-50)
211 die(_("insane git directory %s"), git_dir);
212 memcpy(path, git_dir, len);
214 if (len && path[len-1] != '/')
215 path[len++] = '/';
218 * Create .git/refs/{heads,tags}
220 safe_create_dir(git_path("refs"), 1);
221 safe_create_dir(git_path("refs/heads"), 1);
222 safe_create_dir(git_path("refs/tags"), 1);
224 /* Just look for `init.templatedir` */
225 git_config(git_init_db_config, NULL);
227 /* First copy the templates -- we might have the default
228 * config file there, in which case we would want to read
229 * from it after installing.
231 copy_templates(template_path);
233 git_config(git_default_config, NULL);
234 is_bare_repository_cfg = init_is_bare_repository;
236 /* reading existing config may have overwrote it */
237 if (init_shared_repository != -1)
238 shared_repository = init_shared_repository;
241 * We would have created the above under user's umask -- under
242 * shared-repository settings, we would need to fix them up.
244 if (shared_repository) {
245 adjust_shared_perm(get_git_dir());
246 adjust_shared_perm(git_path("refs"));
247 adjust_shared_perm(git_path("refs/heads"));
248 adjust_shared_perm(git_path("refs/tags"));
252 * Create the default symlink from ".git/HEAD" to the "master"
253 * branch, if it does not exist yet.
255 strcpy(path + len, "HEAD");
256 reinit = (!access(path, R_OK)
257 || readlink(path, junk, sizeof(junk)-1) != -1);
258 if (!reinit) {
259 if (create_symref("HEAD", "refs/heads/master", NULL) < 0)
260 exit(1);
263 /* This forces creation of new config file */
264 sprintf(repo_version_string, "%d", GIT_REPO_VERSION);
265 git_config_set("core.repositoryformatversion", repo_version_string);
267 path[len] = 0;
268 strcpy(path + len, "config");
270 /* Check filemode trustability */
271 filemode = TEST_FILEMODE;
272 if (TEST_FILEMODE && !lstat(path, &st1)) {
273 struct stat st2;
274 filemode = (!chmod(path, st1.st_mode ^ S_IXUSR) &&
275 !lstat(path, &st2) &&
276 st1.st_mode != st2.st_mode &&
277 !chmod(path, st1.st_mode));
278 if (filemode && !reinit && (st1.st_mode & S_IXUSR))
279 filemode = 0;
281 git_config_set("core.filemode", filemode ? "true" : "false");
283 if (is_bare_repository())
284 git_config_set("core.bare", "true");
285 else {
286 const char *work_tree = get_git_work_tree();
287 git_config_set("core.bare", "false");
288 /* allow template config file to override the default */
289 if (log_all_ref_updates == -1)
290 git_config_set("core.logallrefupdates", "true");
291 if (needs_work_tree_config(git_dir, work_tree))
292 git_config_set("core.worktree", work_tree);
295 if (!reinit) {
296 /* Check if symlink is supported in the work tree */
297 path[len] = 0;
298 strcpy(path + len, "tXXXXXX");
299 if (!close(xmkstemp(path)) &&
300 !unlink(path) &&
301 !symlink("testing", path) &&
302 !lstat(path, &st1) &&
303 S_ISLNK(st1.st_mode))
304 unlink(path); /* good */
305 else
306 git_config_set("core.symlinks", "false");
308 /* Check if the filesystem is case-insensitive */
309 path[len] = 0;
310 strcpy(path + len, "CoNfIg");
311 if (!access(path, F_OK))
312 git_config_set("core.ignorecase", "true");
313 probe_utf8_pathname_composition(path, len);
316 return reinit;
319 static void create_object_directory(void)
321 const char *object_directory = get_object_directory();
322 int len = strlen(object_directory);
323 char *path = xmalloc(len + 40);
325 memcpy(path, object_directory, len);
327 safe_create_dir(object_directory, 1);
328 strcpy(path+len, "/pack");
329 safe_create_dir(path, 1);
330 strcpy(path+len, "/info");
331 safe_create_dir(path, 1);
333 free(path);
336 int set_git_dir_init(const char *git_dir, const char *real_git_dir,
337 int exist_ok)
339 if (real_git_dir) {
340 struct stat st;
342 if (!exist_ok && !stat(git_dir, &st))
343 die(_("%s already exists"), git_dir);
345 if (!exist_ok && !stat(real_git_dir, &st))
346 die(_("%s already exists"), real_git_dir);
349 * make sure symlinks are resolved because we'll be
350 * moving the target repo later on in separate_git_dir()
352 git_link = xstrdup(real_path(git_dir));
353 set_git_dir(real_path(real_git_dir));
355 else {
356 set_git_dir(real_path(git_dir));
357 git_link = NULL;
359 return 0;
362 static void separate_git_dir(const char *git_dir)
364 struct stat st;
365 FILE *fp;
367 if (!stat(git_link, &st)) {
368 const char *src;
370 if (S_ISREG(st.st_mode))
371 src = read_gitfile(git_link);
372 else if (S_ISDIR(st.st_mode))
373 src = git_link;
374 else
375 die(_("unable to handle file type %d"), (int)st.st_mode);
377 if (rename(src, git_dir))
378 die_errno(_("unable to move %s to %s"), src, git_dir);
381 fp = fopen(git_link, "w");
382 if (!fp)
383 die(_("Could not create git link %s"), git_link);
384 fprintf(fp, "gitdir: %s\n", git_dir);
385 fclose(fp);
388 int init_db(const char *template_dir, unsigned int flags)
390 int reinit;
391 const char *git_dir = get_git_dir();
393 if (git_link)
394 separate_git_dir(git_dir);
396 safe_create_dir(git_dir, 0);
398 init_is_bare_repository = is_bare_repository();
400 /* Check to see if the repository version is right.
401 * Note that a newly created repository does not have
402 * config file, so this will not fail. What we are catching
403 * is an attempt to reinitialize new repository with an old tool.
405 check_repository_format();
407 reinit = create_default_files(template_dir);
409 create_object_directory();
411 if (shared_repository) {
412 char buf[10];
413 /* We do not spell "group" and such, so that
414 * the configuration can be read by older version
415 * of git. Note, we use octal numbers for new share modes,
416 * and compatibility values for PERM_GROUP and
417 * PERM_EVERYBODY.
419 if (shared_repository < 0)
420 /* force to the mode value */
421 sprintf(buf, "0%o", -shared_repository);
422 else if (shared_repository == PERM_GROUP)
423 sprintf(buf, "%d", OLD_PERM_GROUP);
424 else if (shared_repository == PERM_EVERYBODY)
425 sprintf(buf, "%d", OLD_PERM_EVERYBODY);
426 else
427 die("oops");
428 git_config_set("core.sharedrepository", buf);
429 git_config_set("receive.denyNonFastforwards", "true");
432 if (!(flags & INIT_DB_QUIET)) {
433 int len = strlen(git_dir);
435 /* TRANSLATORS: The first '%s' is either "Reinitialized
436 existing" or "Initialized empty", the second " shared" or
437 "", and the last '%s%s' is the verbatim directory name. */
438 printf(_("%s%s Git repository in %s%s\n"),
439 reinit ? _("Reinitialized existing") : _("Initialized empty"),
440 shared_repository ? _(" shared") : "",
441 git_dir, len && git_dir[len-1] != '/' ? "/" : "");
444 return 0;
447 static int guess_repository_type(const char *git_dir)
449 const char *slash;
450 char *cwd;
451 int cwd_is_git_dir;
454 * "GIT_DIR=. git init" is always bare.
455 * "GIT_DIR=`pwd` git init" too.
457 if (!strcmp(".", git_dir))
458 return 1;
459 cwd = xgetcwd();
460 cwd_is_git_dir = !strcmp(git_dir, cwd);
461 free(cwd);
462 if (cwd_is_git_dir)
463 return 1;
465 * "GIT_DIR=.git or GIT_DIR=something/.git is usually not.
467 if (!strcmp(git_dir, ".git"))
468 return 0;
469 slash = strrchr(git_dir, '/');
470 if (slash && !strcmp(slash, "/.git"))
471 return 0;
474 * Otherwise it is often bare. At this point
475 * we are just guessing.
477 return 1;
480 static int shared_callback(const struct option *opt, const char *arg, int unset)
482 *((int *) opt->value) = (arg) ? git_config_perm("arg", arg) : PERM_GROUP;
483 return 0;
486 static const char *const init_db_usage[] = {
487 N_("git init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]] [<directory>]"),
488 NULL
492 * If you want to, you can share the DB area with any number of branches.
493 * That has advantages: you can save space by sharing all the SHA1 objects.
494 * On the other hand, it might just make lookup slower and messier. You
495 * be the judge. The default case is to have one DB per managed directory.
497 int cmd_init_db(int argc, const char **argv, const char *prefix)
499 const char *git_dir;
500 const char *real_git_dir = NULL;
501 const char *work_tree;
502 const char *template_dir = NULL;
503 unsigned int flags = 0;
504 const struct option init_db_options[] = {
505 OPT_STRING(0, "template", &template_dir, N_("template-directory"),
506 N_("directory from which templates will be used")),
507 OPT_SET_INT(0, "bare", &is_bare_repository_cfg,
508 N_("create a bare repository"), 1),
509 { OPTION_CALLBACK, 0, "shared", &init_shared_repository,
510 N_("permissions"),
511 N_("specify that the git repository is to be shared amongst several users"),
512 PARSE_OPT_OPTARG | PARSE_OPT_NONEG, shared_callback, 0},
513 OPT_BIT('q', "quiet", &flags, N_("be quiet"), INIT_DB_QUIET),
514 OPT_STRING(0, "separate-git-dir", &real_git_dir, N_("gitdir"),
515 N_("separate git dir from working tree")),
516 OPT_END()
519 argc = parse_options(argc, argv, prefix, init_db_options, init_db_usage, 0);
521 if (real_git_dir && !is_absolute_path(real_git_dir))
522 real_git_dir = xstrdup(real_path(real_git_dir));
524 if (argc == 1) {
525 int mkdir_tried = 0;
526 retry:
527 if (chdir(argv[0]) < 0) {
528 if (!mkdir_tried) {
529 int saved;
531 * At this point we haven't read any configuration,
532 * and we know shared_repository should always be 0;
533 * but just in case we play safe.
535 saved = shared_repository;
536 shared_repository = 0;
537 switch (safe_create_leading_directories_const(argv[0])) {
538 case SCLD_OK:
539 case SCLD_PERMS:
540 break;
541 case SCLD_EXISTS:
542 errno = EEXIST;
543 /* fallthru */
544 default:
545 die_errno(_("cannot mkdir %s"), argv[0]);
546 break;
548 shared_repository = saved;
549 if (mkdir(argv[0], 0777) < 0)
550 die_errno(_("cannot mkdir %s"), argv[0]);
551 mkdir_tried = 1;
552 goto retry;
554 die_errno(_("cannot chdir to %s"), argv[0]);
556 } else if (0 < argc) {
557 usage(init_db_usage[0]);
559 if (is_bare_repository_cfg == 1) {
560 char *cwd = xgetcwd();
561 setenv(GIT_DIR_ENVIRONMENT, cwd, argc > 0);
562 free(cwd);
565 if (init_shared_repository != -1)
566 shared_repository = init_shared_repository;
569 * GIT_WORK_TREE makes sense only in conjunction with GIT_DIR
570 * without --bare. Catch the error early.
572 git_dir = getenv(GIT_DIR_ENVIRONMENT);
573 work_tree = getenv(GIT_WORK_TREE_ENVIRONMENT);
574 if ((!git_dir || is_bare_repository_cfg == 1) && work_tree)
575 die(_("%s (or --work-tree=<directory>) not allowed without "
576 "specifying %s (or --git-dir=<directory>)"),
577 GIT_WORK_TREE_ENVIRONMENT,
578 GIT_DIR_ENVIRONMENT);
581 * Set up the default .git directory contents
583 if (!git_dir)
584 git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
586 if (is_bare_repository_cfg < 0)
587 is_bare_repository_cfg = guess_repository_type(git_dir);
589 if (!is_bare_repository_cfg) {
590 const char *git_dir_parent = strrchr(git_dir, '/');
591 if (git_dir_parent) {
592 char *rel = xstrndup(git_dir, git_dir_parent - git_dir);
593 git_work_tree_cfg = xstrdup(real_path(rel));
594 free(rel);
596 if (!git_work_tree_cfg)
597 git_work_tree_cfg = xgetcwd();
598 if (work_tree)
599 set_git_work_tree(work_tree);
600 else
601 set_git_work_tree(git_work_tree_cfg);
602 if (access(get_git_work_tree(), X_OK))
603 die_errno (_("Cannot access work tree '%s'"),
604 get_git_work_tree());
606 else {
607 if (work_tree)
608 set_git_work_tree(work_tree);
611 set_git_dir_init(git_dir, real_git_dir, 1);
613 return init_db(template_dir, flags);