convert trivial sprintf / strcpy calls to xsnprintf
[alt-git.git] / builtin / init-db.c
blobe7d0e31f46e9890a267fa508fa43d184d563afed
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 xsnprintf(repo_version_string, sizeof(repo_version_string),
266 "%d", GIT_REPO_VERSION);
267 git_config_set("core.repositoryformatversion", repo_version_string);
269 path[len] = 0;
270 strcpy(path + len, "config");
272 /* Check filemode trustability */
273 filemode = TEST_FILEMODE;
274 if (TEST_FILEMODE && !lstat(path, &st1)) {
275 struct stat st2;
276 filemode = (!chmod(path, st1.st_mode ^ S_IXUSR) &&
277 !lstat(path, &st2) &&
278 st1.st_mode != st2.st_mode &&
279 !chmod(path, st1.st_mode));
280 if (filemode && !reinit && (st1.st_mode & S_IXUSR))
281 filemode = 0;
283 git_config_set("core.filemode", filemode ? "true" : "false");
285 if (is_bare_repository())
286 git_config_set("core.bare", "true");
287 else {
288 const char *work_tree = get_git_work_tree();
289 git_config_set("core.bare", "false");
290 /* allow template config file to override the default */
291 if (log_all_ref_updates == -1)
292 git_config_set("core.logallrefupdates", "true");
293 if (needs_work_tree_config(git_dir, work_tree))
294 git_config_set("core.worktree", work_tree);
297 if (!reinit) {
298 /* Check if symlink is supported in the work tree */
299 path[len] = 0;
300 strcpy(path + len, "tXXXXXX");
301 if (!close(xmkstemp(path)) &&
302 !unlink(path) &&
303 !symlink("testing", path) &&
304 !lstat(path, &st1) &&
305 S_ISLNK(st1.st_mode))
306 unlink(path); /* good */
307 else
308 git_config_set("core.symlinks", "false");
310 /* Check if the filesystem is case-insensitive */
311 path[len] = 0;
312 strcpy(path + len, "CoNfIg");
313 if (!access(path, F_OK))
314 git_config_set("core.ignorecase", "true");
315 probe_utf8_pathname_composition(path, len);
318 return reinit;
321 static void create_object_directory(void)
323 const char *object_directory = get_object_directory();
324 int len = strlen(object_directory);
325 char *path = xmalloc(len + 40);
327 memcpy(path, object_directory, len);
329 safe_create_dir(object_directory, 1);
330 strcpy(path+len, "/pack");
331 safe_create_dir(path, 1);
332 strcpy(path+len, "/info");
333 safe_create_dir(path, 1);
335 free(path);
338 int set_git_dir_init(const char *git_dir, const char *real_git_dir,
339 int exist_ok)
341 if (real_git_dir) {
342 struct stat st;
344 if (!exist_ok && !stat(git_dir, &st))
345 die(_("%s already exists"), git_dir);
347 if (!exist_ok && !stat(real_git_dir, &st))
348 die(_("%s already exists"), real_git_dir);
351 * make sure symlinks are resolved because we'll be
352 * moving the target repo later on in separate_git_dir()
354 git_link = xstrdup(real_path(git_dir));
355 set_git_dir(real_path(real_git_dir));
357 else {
358 set_git_dir(real_path(git_dir));
359 git_link = NULL;
361 return 0;
364 static void separate_git_dir(const char *git_dir)
366 struct stat st;
368 if (!stat(git_link, &st)) {
369 const char *src;
371 if (S_ISREG(st.st_mode))
372 src = read_gitfile(git_link);
373 else if (S_ISDIR(st.st_mode))
374 src = git_link;
375 else
376 die(_("unable to handle file type %d"), (int)st.st_mode);
378 if (rename(src, git_dir))
379 die_errno(_("unable to move %s to %s"), src, git_dir);
382 write_file(git_link, "gitdir: %s", git_dir);
385 int init_db(const char *template_dir, unsigned int flags)
387 int reinit;
388 const char *git_dir = get_git_dir();
390 if (git_link)
391 separate_git_dir(git_dir);
393 safe_create_dir(git_dir, 0);
395 init_is_bare_repository = is_bare_repository();
397 /* Check to see if the repository version is right.
398 * Note that a newly created repository does not have
399 * config file, so this will not fail. What we are catching
400 * is an attempt to reinitialize new repository with an old tool.
402 check_repository_format();
404 reinit = create_default_files(template_dir);
406 create_object_directory();
408 if (shared_repository) {
409 char buf[10];
410 /* We do not spell "group" and such, so that
411 * the configuration can be read by older version
412 * of git. Note, we use octal numbers for new share modes,
413 * and compatibility values for PERM_GROUP and
414 * PERM_EVERYBODY.
416 if (shared_repository < 0)
417 /* force to the mode value */
418 xsnprintf(buf, sizeof(buf), "0%o", -shared_repository);
419 else if (shared_repository == PERM_GROUP)
420 xsnprintf(buf, sizeof(buf), "%d", OLD_PERM_GROUP);
421 else if (shared_repository == PERM_EVERYBODY)
422 xsnprintf(buf, sizeof(buf), "%d", OLD_PERM_EVERYBODY);
423 else
424 die("BUG: invalid value for shared_repository");
425 git_config_set("core.sharedrepository", buf);
426 git_config_set("receive.denyNonFastforwards", "true");
429 if (!(flags & INIT_DB_QUIET)) {
430 int len = strlen(git_dir);
432 /* TRANSLATORS: The first '%s' is either "Reinitialized
433 existing" or "Initialized empty", the second " shared" or
434 "", and the last '%s%s' is the verbatim directory name. */
435 printf(_("%s%s Git repository in %s%s\n"),
436 reinit ? _("Reinitialized existing") : _("Initialized empty"),
437 shared_repository ? _(" shared") : "",
438 git_dir, len && git_dir[len-1] != '/' ? "/" : "");
441 return 0;
444 static int guess_repository_type(const char *git_dir)
446 const char *slash;
447 char *cwd;
448 int cwd_is_git_dir;
451 * "GIT_DIR=. git init" is always bare.
452 * "GIT_DIR=`pwd` git init" too.
454 if (!strcmp(".", git_dir))
455 return 1;
456 cwd = xgetcwd();
457 cwd_is_git_dir = !strcmp(git_dir, cwd);
458 free(cwd);
459 if (cwd_is_git_dir)
460 return 1;
462 * "GIT_DIR=.git or GIT_DIR=something/.git is usually not.
464 if (!strcmp(git_dir, ".git"))
465 return 0;
466 slash = strrchr(git_dir, '/');
467 if (slash && !strcmp(slash, "/.git"))
468 return 0;
471 * Otherwise it is often bare. At this point
472 * we are just guessing.
474 return 1;
477 static int shared_callback(const struct option *opt, const char *arg, int unset)
479 *((int *) opt->value) = (arg) ? git_config_perm("arg", arg) : PERM_GROUP;
480 return 0;
483 static const char *const init_db_usage[] = {
484 N_("git init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]] [<directory>]"),
485 NULL
489 * If you want to, you can share the DB area with any number of branches.
490 * That has advantages: you can save space by sharing all the SHA1 objects.
491 * On the other hand, it might just make lookup slower and messier. You
492 * be the judge. The default case is to have one DB per managed directory.
494 int cmd_init_db(int argc, const char **argv, const char *prefix)
496 const char *git_dir;
497 const char *real_git_dir = NULL;
498 const char *work_tree;
499 const char *template_dir = NULL;
500 unsigned int flags = 0;
501 const struct option init_db_options[] = {
502 OPT_STRING(0, "template", &template_dir, N_("template-directory"),
503 N_("directory from which templates will be used")),
504 OPT_SET_INT(0, "bare", &is_bare_repository_cfg,
505 N_("create a bare repository"), 1),
506 { OPTION_CALLBACK, 0, "shared", &init_shared_repository,
507 N_("permissions"),
508 N_("specify that the git repository is to be shared amongst several users"),
509 PARSE_OPT_OPTARG | PARSE_OPT_NONEG, shared_callback, 0},
510 OPT_BIT('q', "quiet", &flags, N_("be quiet"), INIT_DB_QUIET),
511 OPT_STRING(0, "separate-git-dir", &real_git_dir, N_("gitdir"),
512 N_("separate git dir from working tree")),
513 OPT_END()
516 argc = parse_options(argc, argv, prefix, init_db_options, init_db_usage, 0);
518 if (real_git_dir && !is_absolute_path(real_git_dir))
519 real_git_dir = xstrdup(real_path(real_git_dir));
521 if (argc == 1) {
522 int mkdir_tried = 0;
523 retry:
524 if (chdir(argv[0]) < 0) {
525 if (!mkdir_tried) {
526 int saved;
528 * At this point we haven't read any configuration,
529 * and we know shared_repository should always be 0;
530 * but just in case we play safe.
532 saved = shared_repository;
533 shared_repository = 0;
534 switch (safe_create_leading_directories_const(argv[0])) {
535 case SCLD_OK:
536 case SCLD_PERMS:
537 break;
538 case SCLD_EXISTS:
539 errno = EEXIST;
540 /* fallthru */
541 default:
542 die_errno(_("cannot mkdir %s"), argv[0]);
543 break;
545 shared_repository = saved;
546 if (mkdir(argv[0], 0777) < 0)
547 die_errno(_("cannot mkdir %s"), argv[0]);
548 mkdir_tried = 1;
549 goto retry;
551 die_errno(_("cannot chdir to %s"), argv[0]);
553 } else if (0 < argc) {
554 usage(init_db_usage[0]);
556 if (is_bare_repository_cfg == 1) {
557 char *cwd = xgetcwd();
558 setenv(GIT_DIR_ENVIRONMENT, cwd, argc > 0);
559 free(cwd);
562 if (init_shared_repository != -1)
563 shared_repository = init_shared_repository;
566 * GIT_WORK_TREE makes sense only in conjunction with GIT_DIR
567 * without --bare. Catch the error early.
569 git_dir = getenv(GIT_DIR_ENVIRONMENT);
570 work_tree = getenv(GIT_WORK_TREE_ENVIRONMENT);
571 if ((!git_dir || is_bare_repository_cfg == 1) && work_tree)
572 die(_("%s (or --work-tree=<directory>) not allowed without "
573 "specifying %s (or --git-dir=<directory>)"),
574 GIT_WORK_TREE_ENVIRONMENT,
575 GIT_DIR_ENVIRONMENT);
578 * Set up the default .git directory contents
580 if (!git_dir)
581 git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
583 if (is_bare_repository_cfg < 0)
584 is_bare_repository_cfg = guess_repository_type(git_dir);
586 if (!is_bare_repository_cfg) {
587 const char *git_dir_parent = strrchr(git_dir, '/');
588 if (git_dir_parent) {
589 char *rel = xstrndup(git_dir, git_dir_parent - git_dir);
590 git_work_tree_cfg = xstrdup(real_path(rel));
591 free(rel);
593 if (!git_work_tree_cfg)
594 git_work_tree_cfg = xgetcwd();
595 if (work_tree)
596 set_git_work_tree(work_tree);
597 else
598 set_git_work_tree(git_work_tree_cfg);
599 if (access(get_git_work_tree(), X_OK))
600 die_errno (_("Cannot access work tree '%s'"),
601 get_git_work_tree());
603 else {
604 if (work_tree)
605 set_git_work_tree(work_tree);
608 set_git_dir_init(git_dir, real_git_dir, 1);
610 return init_db(template_dir, flags);