i18n: git-branch "remote branch '%s' not found" message
[git/jnareb-git.git] / builtin / init-db.c
blobe0e5ce3b1985cd2aba66d8c00d134d5c7a5a62ef
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;
25 static void safe_create_dir(const char *dir, int share)
27 if (mkdir(dir, 0777) < 0) {
28 if (errno != EEXIST) {
29 perror(dir);
30 exit(1);
33 else if (share && adjust_shared_perm(dir))
34 die(_("Could not make %s writable by group"), dir);
37 static void copy_templates_1(char *path, int baselen,
38 char *template, int template_baselen,
39 DIR *dir)
41 struct dirent *de;
43 /* Note: if ".git/hooks" file exists in the repository being
44 * re-initialized, /etc/core-git/templates/hooks/update would
45 * cause "git init" to fail here. I think this is sane but
46 * it means that the set of templates we ship by default, along
47 * with the way the namespace under .git/ is organized, should
48 * be really carefully chosen.
50 safe_create_dir(path, 1);
51 while ((de = readdir(dir)) != NULL) {
52 struct stat st_git, st_template;
53 int namelen;
54 int exists = 0;
56 if (de->d_name[0] == '.')
57 continue;
58 namelen = strlen(de->d_name);
59 if ((PATH_MAX <= baselen + namelen) ||
60 (PATH_MAX <= template_baselen + namelen))
61 die(_("insanely long template name %s"), de->d_name);
62 memcpy(path + baselen, de->d_name, namelen+1);
63 memcpy(template + template_baselen, de->d_name, namelen+1);
64 if (lstat(path, &st_git)) {
65 if (errno != ENOENT)
66 die_errno(_("cannot stat '%s'"), path);
68 else
69 exists = 1;
71 if (lstat(template, &st_template))
72 die_errno(_("cannot stat template '%s'"), template);
74 if (S_ISDIR(st_template.st_mode)) {
75 DIR *subdir = opendir(template);
76 int baselen_sub = baselen + namelen;
77 int template_baselen_sub = template_baselen + namelen;
78 if (!subdir)
79 die_errno(_("cannot opendir '%s'"), template);
80 path[baselen_sub++] =
81 template[template_baselen_sub++] = '/';
82 path[baselen_sub] =
83 template[template_baselen_sub] = 0;
84 copy_templates_1(path, baselen_sub,
85 template, template_baselen_sub,
86 subdir);
87 closedir(subdir);
89 else if (exists)
90 continue;
91 else if (S_ISLNK(st_template.st_mode)) {
92 char lnk[256];
93 int len;
94 len = readlink(template, lnk, sizeof(lnk));
95 if (len < 0)
96 die_errno(_("cannot readlink '%s'"), template);
97 if (sizeof(lnk) <= len)
98 die(_("insanely long symlink %s"), template);
99 lnk[len] = 0;
100 if (symlink(lnk, path))
101 die_errno(_("cannot symlink '%s' '%s'"), lnk, path);
103 else if (S_ISREG(st_template.st_mode)) {
104 if (copy_file(path, template, st_template.st_mode))
105 die_errno(_("cannot copy '%s' to '%s'"), template,
106 path);
108 else
109 error(_("ignoring template %s"), template);
113 static void copy_templates(const char *template_dir)
115 char path[PATH_MAX];
116 char template_path[PATH_MAX];
117 int template_len;
118 DIR *dir;
119 const char *git_dir = get_git_dir();
120 int len = strlen(git_dir);
122 if (!template_dir)
123 template_dir = getenv(TEMPLATE_DIR_ENVIRONMENT);
124 if (!template_dir)
125 template_dir = init_db_template_dir;
126 if (!template_dir)
127 template_dir = system_path(DEFAULT_GIT_TEMPLATE_DIR);
128 if (!template_dir[0])
129 return;
130 template_len = strlen(template_dir);
131 if (PATH_MAX <= (template_len+strlen("/config")))
132 die(_("insanely long template path %s"), template_dir);
133 strcpy(template_path, template_dir);
134 if (template_path[template_len-1] != '/') {
135 template_path[template_len++] = '/';
136 template_path[template_len] = 0;
138 dir = opendir(template_path);
139 if (!dir) {
140 warning(_("templates not found %s"), template_dir);
141 return;
144 /* Make sure that template is from the correct vintage */
145 strcpy(template_path + template_len, "config");
146 repository_format_version = 0;
147 git_config_from_file(check_repository_format_version,
148 template_path, NULL);
149 template_path[template_len] = 0;
151 if (repository_format_version &&
152 repository_format_version != GIT_REPO_VERSION) {
153 warning(_("not copying templates of "
154 "a wrong format version %d from '%s'"),
155 repository_format_version,
156 template_dir);
157 closedir(dir);
158 return;
161 memcpy(path, git_dir, len);
162 if (len && path[len - 1] != '/')
163 path[len++] = '/';
164 path[len] = 0;
165 copy_templates_1(path, len,
166 template_path, template_len,
167 dir);
168 closedir(dir);
171 static int git_init_db_config(const char *k, const char *v, void *cb)
173 if (!strcmp(k, "init.templatedir"))
174 return git_config_pathname(&init_db_template_dir, k, v);
176 return 0;
179 static int create_default_files(const char *template_path)
181 const char *git_dir = get_git_dir();
182 unsigned len = strlen(git_dir);
183 static char path[PATH_MAX];
184 struct stat st1;
185 char repo_version_string[10];
186 char junk[2];
187 int reinit;
188 int filemode;
190 if (len > sizeof(path)-50)
191 die(_("insane git directory %s"), git_dir);
192 memcpy(path, git_dir, len);
194 if (len && path[len-1] != '/')
195 path[len++] = '/';
198 * Create .git/refs/{heads,tags}
200 safe_create_dir(git_path("refs"), 1);
201 safe_create_dir(git_path("refs/heads"), 1);
202 safe_create_dir(git_path("refs/tags"), 1);
204 /* Just look for `init.templatedir` */
205 git_config(git_init_db_config, NULL);
207 /* First copy the templates -- we might have the default
208 * config file there, in which case we would want to read
209 * from it after installing.
211 copy_templates(template_path);
213 git_config(git_default_config, NULL);
214 is_bare_repository_cfg = init_is_bare_repository;
216 /* reading existing config may have overwrote it */
217 if (init_shared_repository != -1)
218 shared_repository = init_shared_repository;
221 * We would have created the above under user's umask -- under
222 * shared-repository settings, we would need to fix them up.
224 if (shared_repository) {
225 adjust_shared_perm(get_git_dir());
226 adjust_shared_perm(git_path("refs"));
227 adjust_shared_perm(git_path("refs/heads"));
228 adjust_shared_perm(git_path("refs/tags"));
232 * Create the default symlink from ".git/HEAD" to the "master"
233 * branch, if it does not exist yet.
235 strcpy(path + len, "HEAD");
236 reinit = (!access(path, R_OK)
237 || readlink(path, junk, sizeof(junk)-1) != -1);
238 if (!reinit) {
239 if (create_symref("HEAD", "refs/heads/master", NULL) < 0)
240 exit(1);
243 /* This forces creation of new config file */
244 sprintf(repo_version_string, "%d", GIT_REPO_VERSION);
245 git_config_set("core.repositoryformatversion", repo_version_string);
247 path[len] = 0;
248 strcpy(path + len, "config");
250 /* Check filemode trustability */
251 filemode = TEST_FILEMODE;
252 if (TEST_FILEMODE && !lstat(path, &st1)) {
253 struct stat st2;
254 filemode = (!chmod(path, st1.st_mode ^ S_IXUSR) &&
255 !lstat(path, &st2) &&
256 st1.st_mode != st2.st_mode);
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 == -1)
267 git_config_set("core.logallrefupdates", "true");
268 if (prefixcmp(git_dir, work_tree) ||
269 strcmp(git_dir + strlen(work_tree), "/.git")) {
270 git_config_set("core.worktree", work_tree);
274 if (!reinit) {
275 /* Check if symlink is supported in the work tree */
276 path[len] = 0;
277 strcpy(path + len, "tXXXXXX");
278 if (!close(xmkstemp(path)) &&
279 !unlink(path) &&
280 !symlink("testing", path) &&
281 !lstat(path, &st1) &&
282 S_ISLNK(st1.st_mode))
283 unlink(path); /* good */
284 else
285 git_config_set("core.symlinks", "false");
287 /* Check if the filesystem is case-insensitive */
288 path[len] = 0;
289 strcpy(path + len, "CoNfIg");
290 if (!access(path, F_OK))
291 git_config_set("core.ignorecase", "true");
294 return reinit;
297 static void create_object_directory(void)
299 const char *object_directory = get_object_directory();
300 int len = strlen(object_directory);
301 char *path = xmalloc(len + 40);
303 memcpy(path, object_directory, len);
305 safe_create_dir(object_directory, 1);
306 strcpy(path+len, "/pack");
307 safe_create_dir(path, 1);
308 strcpy(path+len, "/info");
309 safe_create_dir(path, 1);
311 free(path);
314 int init_db(const char *template_dir, unsigned int flags)
316 int reinit;
318 safe_create_dir(get_git_dir(), 0);
320 init_is_bare_repository = is_bare_repository();
322 /* Check to see if the repository version is right.
323 * Note that a newly created repository does not have
324 * config file, so this will not fail. What we are catching
325 * is an attempt to reinitialize new repository with an old tool.
327 check_repository_format();
329 reinit = create_default_files(template_dir);
331 create_object_directory();
333 if (shared_repository) {
334 char buf[10];
335 /* We do not spell "group" and such, so that
336 * the configuration can be read by older version
337 * of git. Note, we use octal numbers for new share modes,
338 * and compatibility values for PERM_GROUP and
339 * PERM_EVERYBODY.
341 if (shared_repository < 0)
342 /* force to the mode value */
343 sprintf(buf, "0%o", -shared_repository);
344 else if (shared_repository == PERM_GROUP)
345 sprintf(buf, "%d", OLD_PERM_GROUP);
346 else if (shared_repository == PERM_EVERYBODY)
347 sprintf(buf, "%d", OLD_PERM_EVERYBODY);
348 else
349 die("oops");
350 git_config_set("core.sharedrepository", buf);
351 git_config_set("receive.denyNonFastforwards", "true");
354 if (!(flags & INIT_DB_QUIET)) {
355 const char *git_dir = get_git_dir();
356 int len = strlen(git_dir);
359 * TRANSLATORS: The first '%s' is either "Reinitialized
360 * existing" or "Initialized empty", the second " shared" or
361 * "", and the last '%s%s' is the verbatim directory name.
363 printf(_("%s%s Git repository in %s%s\n"),
364 reinit ? _("Reinitialized existing") : _("Initialized empty"),
365 shared_repository ? _(" shared") : "",
366 git_dir, len && git_dir[len-1] != '/' ? "/" : "");
369 return 0;
372 static int guess_repository_type(const char *git_dir)
374 char cwd[PATH_MAX];
375 const char *slash;
378 * "GIT_DIR=. git init" is always bare.
379 * "GIT_DIR=`pwd` git init" too.
381 if (!strcmp(".", git_dir))
382 return 1;
383 if (!getcwd(cwd, sizeof(cwd)))
384 die_errno(_("cannot tell cwd"));
385 if (!strcmp(git_dir, cwd))
386 return 1;
388 * "GIT_DIR=.git or GIT_DIR=something/.git is usually not.
390 if (!strcmp(git_dir, ".git"))
391 return 0;
392 slash = strrchr(git_dir, '/');
393 if (slash && !strcmp(slash, "/.git"))
394 return 0;
397 * Otherwise it is often bare. At this point
398 * we are just guessing.
400 return 1;
403 static int shared_callback(const struct option *opt, const char *arg, int unset)
405 *((int *) opt->value) = (arg) ? git_config_perm("arg", arg) : PERM_GROUP;
406 return 0;
409 static const char *const init_db_usage[] = {
410 "git init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]] [directory]",
411 NULL
415 * If you want to, you can share the DB area with any number of branches.
416 * That has advantages: you can save space by sharing all the SHA1 objects.
417 * On the other hand, it might just make lookup slower and messier. You
418 * be the judge. The default case is to have one DB per managed directory.
420 int cmd_init_db(int argc, const char **argv, const char *prefix)
422 const char *git_dir;
423 const char *work_tree;
424 const char *template_dir = NULL;
425 unsigned int flags = 0;
426 const struct option init_db_options[] = {
427 OPT_STRING(0, "template", &template_dir, "template-directory",
428 "directory from which templates will be used"),
429 OPT_SET_INT(0, "bare", &is_bare_repository_cfg,
430 "create a bare repository", 1),
431 { OPTION_CALLBACK, 0, "shared", &init_shared_repository,
432 "permissions",
433 "specify that the git repository is to be shared amongst several users",
434 PARSE_OPT_OPTARG | PARSE_OPT_NONEG, shared_callback, 0},
435 OPT_BIT('q', "quiet", &flags, "be quiet", INIT_DB_QUIET),
436 OPT_END()
439 argc = parse_options(argc, argv, prefix, init_db_options, init_db_usage, 0);
441 if (argc == 1) {
442 int mkdir_tried = 0;
443 retry:
444 if (chdir(argv[0]) < 0) {
445 if (!mkdir_tried) {
446 int saved;
448 * At this point we haven't read any configuration,
449 * and we know shared_repository should always be 0;
450 * but just in case we play safe.
452 saved = shared_repository;
453 shared_repository = 0;
454 switch (safe_create_leading_directories_const(argv[0])) {
455 case -3:
456 errno = EEXIST;
457 /* fallthru */
458 case -1:
459 die_errno(_("cannot mkdir %s"), argv[0]);
460 break;
461 default:
462 break;
464 shared_repository = saved;
465 if (mkdir(argv[0], 0777) < 0)
466 die_errno(_("cannot mkdir %s"), argv[0]);
467 mkdir_tried = 1;
468 goto retry;
470 die_errno(_("cannot chdir to %s"), argv[0]);
472 } else if (0 < argc) {
473 usage(init_db_usage[0]);
475 if (is_bare_repository_cfg == 1) {
476 static char git_dir[PATH_MAX+1];
478 setenv(GIT_DIR_ENVIRONMENT,
479 getcwd(git_dir, sizeof(git_dir)), argc > 0);
482 if (init_shared_repository != -1)
483 shared_repository = init_shared_repository;
486 * GIT_WORK_TREE makes sense only in conjunction with GIT_DIR
487 * without --bare. Catch the error early.
489 git_dir = getenv(GIT_DIR_ENVIRONMENT);
490 work_tree = getenv(GIT_WORK_TREE_ENVIRONMENT);
491 if ((!git_dir || is_bare_repository_cfg == 1) && work_tree)
492 die(_("%s (or --work-tree=<directory>) not allowed without "
493 "specifying %s (or --git-dir=<directory>)"),
494 GIT_WORK_TREE_ENVIRONMENT,
495 GIT_DIR_ENVIRONMENT);
498 * Set up the default .git directory contents
500 if (!git_dir)
501 git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
503 if (is_bare_repository_cfg < 0)
504 is_bare_repository_cfg = guess_repository_type(git_dir);
506 if (!is_bare_repository_cfg) {
507 if (git_dir) {
508 const char *git_dir_parent = strrchr(git_dir, '/');
509 if (git_dir_parent) {
510 char *rel = xstrndup(git_dir, git_dir_parent - git_dir);
511 git_work_tree_cfg = xstrdup(make_absolute_path(rel));
512 free(rel);
515 if (!git_work_tree_cfg) {
516 git_work_tree_cfg = xcalloc(PATH_MAX, 1);
517 if (!getcwd(git_work_tree_cfg, PATH_MAX))
518 die_errno (_("Cannot access current working directory"));
520 if (work_tree)
521 set_git_work_tree(make_absolute_path(work_tree));
522 else
523 set_git_work_tree(git_work_tree_cfg);
524 if (access(get_git_work_tree(), X_OK))
525 die_errno (_("Cannot access work tree '%s'"),
526 get_git_work_tree());
528 else {
529 if (work_tree)
530 set_git_work_tree(make_absolute_path(work_tree));
533 set_git_dir(make_absolute_path(git_dir));
535 return init_db(template_dir, flags);