Merge branch 'ls/p4-keep-empty-commits' into maint
[git/debian.git] / builtin / init-db.c
blob69323e186cda73fe02658a535d8eae8387ff5444
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(char *path, int baselen,
40 char *template, int template_baselen,
41 DIR *dir)
43 struct dirent *de;
45 /* Note: if ".git/hooks" file exists in the repository being
46 * re-initialized, /etc/core-git/templates/hooks/update would
47 * cause "git init" to fail here. I think this is sane but
48 * it means that the set of templates we ship by default, along
49 * with the way the namespace under .git/ is organized, should
50 * be really carefully chosen.
52 safe_create_dir(path, 1);
53 while ((de = readdir(dir)) != NULL) {
54 struct stat st_git, st_template;
55 int namelen;
56 int exists = 0;
58 if (de->d_name[0] == '.')
59 continue;
60 namelen = strlen(de->d_name);
61 if ((PATH_MAX <= baselen + namelen) ||
62 (PATH_MAX <= template_baselen + namelen))
63 die(_("insanely long template name %s"), de->d_name);
64 memcpy(path + baselen, de->d_name, namelen+1);
65 memcpy(template + template_baselen, de->d_name, namelen+1);
66 if (lstat(path, &st_git)) {
67 if (errno != ENOENT)
68 die_errno(_("cannot stat '%s'"), path);
70 else
71 exists = 1;
73 if (lstat(template, &st_template))
74 die_errno(_("cannot stat template '%s'"), template);
76 if (S_ISDIR(st_template.st_mode)) {
77 DIR *subdir = opendir(template);
78 int baselen_sub = baselen + namelen;
79 int template_baselen_sub = template_baselen + namelen;
80 if (!subdir)
81 die_errno(_("cannot opendir '%s'"), template);
82 path[baselen_sub++] =
83 template[template_baselen_sub++] = '/';
84 path[baselen_sub] =
85 template[template_baselen_sub] = 0;
86 copy_templates_1(path, baselen_sub,
87 template, template_baselen_sub,
88 subdir);
89 closedir(subdir);
91 else if (exists)
92 continue;
93 else if (S_ISLNK(st_template.st_mode)) {
94 char lnk[256];
95 int len;
96 len = readlink(template, lnk, sizeof(lnk));
97 if (len < 0)
98 die_errno(_("cannot readlink '%s'"), template);
99 if (sizeof(lnk) <= len)
100 die(_("insanely long symlink %s"), template);
101 lnk[len] = 0;
102 if (symlink(lnk, path))
103 die_errno(_("cannot symlink '%s' '%s'"), lnk, path);
105 else if (S_ISREG(st_template.st_mode)) {
106 if (copy_file(path, template, st_template.st_mode))
107 die_errno(_("cannot copy '%s' to '%s'"), template,
108 path);
110 else
111 error(_("ignoring template %s"), template);
115 static void copy_templates(const char *template_dir)
117 char path[PATH_MAX];
118 char template_path[PATH_MAX];
119 int template_len;
120 DIR *dir;
121 const char *git_dir = get_git_dir();
122 int len = strlen(git_dir);
123 char *to_free = NULL;
125 if (!template_dir)
126 template_dir = getenv(TEMPLATE_DIR_ENVIRONMENT);
127 if (!template_dir)
128 template_dir = init_db_template_dir;
129 if (!template_dir)
130 template_dir = to_free = system_path(DEFAULT_GIT_TEMPLATE_DIR);
131 if (!template_dir[0]) {
132 free(to_free);
133 return;
135 template_len = strlen(template_dir);
136 if (PATH_MAX <= (template_len+strlen("/config")))
137 die(_("insanely long template path %s"), template_dir);
138 strcpy(template_path, template_dir);
139 if (template_path[template_len-1] != '/') {
140 template_path[template_len++] = '/';
141 template_path[template_len] = 0;
143 dir = opendir(template_path);
144 if (!dir) {
145 warning(_("templates not found %s"), template_dir);
146 goto free_return;
149 /* Make sure that template is from the correct vintage */
150 strcpy(template_path + template_len, "config");
151 repository_format_version = 0;
152 git_config_from_file(check_repository_format_version,
153 template_path, NULL);
154 template_path[template_len] = 0;
156 if (repository_format_version &&
157 repository_format_version != GIT_REPO_VERSION) {
158 warning(_("not copying templates of "
159 "a wrong format version %d from '%s'"),
160 repository_format_version,
161 template_dir);
162 goto close_free_return;
165 memcpy(path, git_dir, len);
166 if (len && path[len - 1] != '/')
167 path[len++] = '/';
168 path[len] = 0;
169 copy_templates_1(path, len,
170 template_path, template_len,
171 dir);
172 close_free_return:
173 closedir(dir);
174 free_return:
175 free(to_free);
178 static int git_init_db_config(const char *k, const char *v, void *cb)
180 if (!strcmp(k, "init.templatedir"))
181 return git_config_pathname(&init_db_template_dir, k, v);
183 return 0;
187 * If the git_dir is not directly inside the working tree, then git will not
188 * find it by default, and we need to set the worktree explicitly.
190 static int needs_work_tree_config(const char *git_dir, const char *work_tree)
192 if (!strcmp(work_tree, "/") && !strcmp(git_dir, "/.git"))
193 return 0;
194 if (skip_prefix(git_dir, work_tree, &git_dir) &&
195 !strcmp(git_dir, "/.git"))
196 return 0;
197 return 1;
200 static int create_default_files(const char *template_path)
202 const char *git_dir = get_git_dir();
203 unsigned len = strlen(git_dir);
204 static char path[PATH_MAX];
205 struct stat st1;
206 char repo_version_string[10];
207 char junk[2];
208 int reinit;
209 int filemode;
211 if (len > sizeof(path)-50)
212 die(_("insane git directory %s"), git_dir);
213 memcpy(path, git_dir, len);
215 if (len && path[len-1] != '/')
216 path[len++] = '/';
219 * Create .git/refs/{heads,tags}
221 safe_create_dir(git_path("refs"), 1);
222 safe_create_dir(git_path("refs/heads"), 1);
223 safe_create_dir(git_path("refs/tags"), 1);
225 /* Just look for `init.templatedir` */
226 git_config(git_init_db_config, NULL);
228 /* First copy the templates -- we might have the default
229 * config file there, in which case we would want to read
230 * from it after installing.
232 copy_templates(template_path);
234 git_config(git_default_config, NULL);
235 is_bare_repository_cfg = init_is_bare_repository;
237 /* reading existing config may have overwrote it */
238 if (init_shared_repository != -1)
239 shared_repository = init_shared_repository;
242 * We would have created the above under user's umask -- under
243 * shared-repository settings, we would need to fix them up.
245 if (shared_repository) {
246 adjust_shared_perm(get_git_dir());
247 adjust_shared_perm(git_path("refs"));
248 adjust_shared_perm(git_path("refs/heads"));
249 adjust_shared_perm(git_path("refs/tags"));
253 * Create the default symlink from ".git/HEAD" to the "master"
254 * branch, if it does not exist yet.
256 strcpy(path + len, "HEAD");
257 reinit = (!access(path, R_OK)
258 || readlink(path, junk, sizeof(junk)-1) != -1);
259 if (!reinit) {
260 if (create_symref("HEAD", "refs/heads/master", NULL) < 0)
261 exit(1);
264 /* This forces creation of new config file */
265 sprintf(repo_version_string, "%d", GIT_REPO_VERSION);
266 git_config_set("core.repositoryformatversion", repo_version_string);
268 path[len] = 0;
269 strcpy(path + len, "config");
271 /* Check filemode trustability */
272 filemode = TEST_FILEMODE;
273 if (TEST_FILEMODE && !lstat(path, &st1)) {
274 struct stat st2;
275 filemode = (!chmod(path, st1.st_mode ^ S_IXUSR) &&
276 !lstat(path, &st2) &&
277 st1.st_mode != st2.st_mode &&
278 !chmod(path, st1.st_mode));
279 if (filemode && !reinit && (st1.st_mode & S_IXUSR))
280 filemode = 0;
282 git_config_set("core.filemode", filemode ? "true" : "false");
284 if (is_bare_repository())
285 git_config_set("core.bare", "true");
286 else {
287 const char *work_tree = get_git_work_tree();
288 git_config_set("core.bare", "false");
289 /* allow template config file to override the default */
290 if (log_all_ref_updates == -1)
291 git_config_set("core.logallrefupdates", "true");
292 if (needs_work_tree_config(git_dir, work_tree))
293 git_config_set("core.worktree", work_tree);
296 if (!reinit) {
297 /* Check if symlink is supported in the work tree */
298 path[len] = 0;
299 strcpy(path + len, "tXXXXXX");
300 if (!close(xmkstemp(path)) &&
301 !unlink(path) &&
302 !symlink("testing", path) &&
303 !lstat(path, &st1) &&
304 S_ISLNK(st1.st_mode))
305 unlink(path); /* good */
306 else
307 git_config_set("core.symlinks", "false");
309 /* Check if the filesystem is case-insensitive */
310 path[len] = 0;
311 strcpy(path + len, "CoNfIg");
312 if (!access(path, F_OK))
313 git_config_set("core.ignorecase", "true");
314 probe_utf8_pathname_composition(path, len);
317 return reinit;
320 static void create_object_directory(void)
322 const char *object_directory = get_object_directory();
323 int len = strlen(object_directory);
324 char *path = xmalloc(len + 40);
326 memcpy(path, object_directory, len);
328 safe_create_dir(object_directory, 1);
329 strcpy(path+len, "/pack");
330 safe_create_dir(path, 1);
331 strcpy(path+len, "/info");
332 safe_create_dir(path, 1);
334 free(path);
337 int set_git_dir_init(const char *git_dir, const char *real_git_dir,
338 int exist_ok)
340 if (real_git_dir) {
341 struct stat st;
343 if (!exist_ok && !stat(git_dir, &st))
344 die(_("%s already exists"), git_dir);
346 if (!exist_ok && !stat(real_git_dir, &st))
347 die(_("%s already exists"), real_git_dir);
350 * make sure symlinks are resolved because we'll be
351 * moving the target repo later on in separate_git_dir()
353 git_link = xstrdup(real_path(git_dir));
354 set_git_dir(real_path(real_git_dir));
356 else {
357 set_git_dir(real_path(git_dir));
358 git_link = NULL;
360 return 0;
363 static void separate_git_dir(const char *git_dir)
365 struct stat st;
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 write_file(git_link, "gitdir: %s", git_dir);
384 int init_db(const char *template_dir, unsigned int flags)
386 int reinit;
387 const char *git_dir = get_git_dir();
389 if (git_link)
390 separate_git_dir(git_dir);
392 safe_create_dir(git_dir, 0);
394 init_is_bare_repository = is_bare_repository();
396 /* Check to see if the repository version is right.
397 * Note that a newly created repository does not have
398 * config file, so this will not fail. What we are catching
399 * is an attempt to reinitialize new repository with an old tool.
401 check_repository_format();
403 reinit = create_default_files(template_dir);
405 create_object_directory();
407 if (shared_repository) {
408 char buf[10];
409 /* We do not spell "group" and such, so that
410 * the configuration can be read by older version
411 * of git. Note, we use octal numbers for new share modes,
412 * and compatibility values for PERM_GROUP and
413 * PERM_EVERYBODY.
415 if (shared_repository < 0)
416 /* force to the mode value */
417 sprintf(buf, "0%o", -shared_repository);
418 else if (shared_repository == PERM_GROUP)
419 sprintf(buf, "%d", OLD_PERM_GROUP);
420 else if (shared_repository == PERM_EVERYBODY)
421 sprintf(buf, "%d", OLD_PERM_EVERYBODY);
422 else
423 die("oops");
424 git_config_set("core.sharedrepository", buf);
425 git_config_set("receive.denyNonFastforwards", "true");
428 if (!(flags & INIT_DB_QUIET)) {
429 int len = strlen(git_dir);
431 /* TRANSLATORS: The first '%s' is either "Reinitialized
432 existing" or "Initialized empty", the second " shared" or
433 "", and the last '%s%s' is the verbatim directory name. */
434 printf(_("%s%s Git repository in %s%s\n"),
435 reinit ? _("Reinitialized existing") : _("Initialized empty"),
436 shared_repository ? _(" shared") : "",
437 git_dir, len && git_dir[len-1] != '/' ? "/" : "");
440 return 0;
443 static int guess_repository_type(const char *git_dir)
445 const char *slash;
446 char *cwd;
447 int cwd_is_git_dir;
450 * "GIT_DIR=. git init" is always bare.
451 * "GIT_DIR=`pwd` git init" too.
453 if (!strcmp(".", git_dir))
454 return 1;
455 cwd = xgetcwd();
456 cwd_is_git_dir = !strcmp(git_dir, cwd);
457 free(cwd);
458 if (cwd_is_git_dir)
459 return 1;
461 * "GIT_DIR=.git or GIT_DIR=something/.git is usually not.
463 if (!strcmp(git_dir, ".git"))
464 return 0;
465 slash = strrchr(git_dir, '/');
466 if (slash && !strcmp(slash, "/.git"))
467 return 0;
470 * Otherwise it is often bare. At this point
471 * we are just guessing.
473 return 1;
476 static int shared_callback(const struct option *opt, const char *arg, int unset)
478 *((int *) opt->value) = (arg) ? git_config_perm("arg", arg) : PERM_GROUP;
479 return 0;
482 static const char *const init_db_usage[] = {
483 N_("git init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]] [<directory>]"),
484 NULL
488 * If you want to, you can share the DB area with any number of branches.
489 * That has advantages: you can save space by sharing all the SHA1 objects.
490 * On the other hand, it might just make lookup slower and messier. You
491 * be the judge. The default case is to have one DB per managed directory.
493 int cmd_init_db(int argc, const char **argv, const char *prefix)
495 const char *git_dir;
496 const char *real_git_dir = NULL;
497 const char *work_tree;
498 const char *template_dir = NULL;
499 unsigned int flags = 0;
500 const struct option init_db_options[] = {
501 OPT_STRING(0, "template", &template_dir, N_("template-directory"),
502 N_("directory from which templates will be used")),
503 OPT_SET_INT(0, "bare", &is_bare_repository_cfg,
504 N_("create a bare repository"), 1),
505 { OPTION_CALLBACK, 0, "shared", &init_shared_repository,
506 N_("permissions"),
507 N_("specify that the git repository is to be shared amongst several users"),
508 PARSE_OPT_OPTARG | PARSE_OPT_NONEG, shared_callback, 0},
509 OPT_BIT('q', "quiet", &flags, N_("be quiet"), INIT_DB_QUIET),
510 OPT_STRING(0, "separate-git-dir", &real_git_dir, N_("gitdir"),
511 N_("separate git dir from working tree")),
512 OPT_END()
515 argc = parse_options(argc, argv, prefix, init_db_options, init_db_usage, 0);
517 if (real_git_dir && !is_absolute_path(real_git_dir))
518 real_git_dir = xstrdup(real_path(real_git_dir));
520 if (argc == 1) {
521 int mkdir_tried = 0;
522 retry:
523 if (chdir(argv[0]) < 0) {
524 if (!mkdir_tried) {
525 int saved;
527 * At this point we haven't read any configuration,
528 * and we know shared_repository should always be 0;
529 * but just in case we play safe.
531 saved = shared_repository;
532 shared_repository = 0;
533 switch (safe_create_leading_directories_const(argv[0])) {
534 case SCLD_OK:
535 case SCLD_PERMS:
536 break;
537 case SCLD_EXISTS:
538 errno = EEXIST;
539 /* fallthru */
540 default:
541 die_errno(_("cannot mkdir %s"), argv[0]);
542 break;
544 shared_repository = saved;
545 if (mkdir(argv[0], 0777) < 0)
546 die_errno(_("cannot mkdir %s"), argv[0]);
547 mkdir_tried = 1;
548 goto retry;
550 die_errno(_("cannot chdir to %s"), argv[0]);
552 } else if (0 < argc) {
553 usage(init_db_usage[0]);
555 if (is_bare_repository_cfg == 1) {
556 char *cwd = xgetcwd();
557 setenv(GIT_DIR_ENVIRONMENT, cwd, argc > 0);
558 free(cwd);
561 if (init_shared_repository != -1)
562 shared_repository = init_shared_repository;
565 * GIT_WORK_TREE makes sense only in conjunction with GIT_DIR
566 * without --bare. Catch the error early.
568 git_dir = getenv(GIT_DIR_ENVIRONMENT);
569 work_tree = getenv(GIT_WORK_TREE_ENVIRONMENT);
570 if ((!git_dir || is_bare_repository_cfg == 1) && work_tree)
571 die(_("%s (or --work-tree=<directory>) not allowed without "
572 "specifying %s (or --git-dir=<directory>)"),
573 GIT_WORK_TREE_ENVIRONMENT,
574 GIT_DIR_ENVIRONMENT);
577 * Set up the default .git directory contents
579 if (!git_dir)
580 git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
582 if (is_bare_repository_cfg < 0)
583 is_bare_repository_cfg = guess_repository_type(git_dir);
585 if (!is_bare_repository_cfg) {
586 const char *git_dir_parent = strrchr(git_dir, '/');
587 if (git_dir_parent) {
588 char *rel = xstrndup(git_dir, git_dir_parent - git_dir);
589 git_work_tree_cfg = xstrdup(real_path(rel));
590 free(rel);
592 if (!git_work_tree_cfg)
593 git_work_tree_cfg = xgetcwd();
594 if (work_tree)
595 set_git_work_tree(work_tree);
596 else
597 set_git_work_tree(git_work_tree_cfg);
598 if (access(get_git_work_tree(), X_OK))
599 die_errno (_("Cannot access work tree '%s'"),
600 get_git_work_tree());
602 else {
603 if (work_tree)
604 set_git_work_tree(work_tree);
607 set_git_dir_init(git_dir, real_git_dir, 1);
609 return init_db(template_dir, flags);