Merge git://git.bogomips.org/git-svn
[git/git-p4.git] / builtin-init-db.c
blobbaf0d09ac4ea372b4015d399560a133b401b55cc
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"
10 #ifndef DEFAULT_GIT_TEMPLATE_DIR
11 #define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates"
12 #endif
14 #ifdef NO_TRUSTABLE_FILEMODE
15 #define TEST_FILEMODE 0
16 #else
17 #define TEST_FILEMODE 1
18 #endif
20 static void safe_create_dir(const char *dir, int share)
22 if (mkdir(dir, 0777) < 0) {
23 if (errno != EEXIST) {
24 perror(dir);
25 exit(1);
28 else if (share && adjust_shared_perm(dir))
29 die("Could not make %s writable by group\n", dir);
32 static void copy_templates_1(char *path, int baselen,
33 char *template, int template_baselen,
34 DIR *dir)
36 struct dirent *de;
38 /* Note: if ".git/hooks" file exists in the repository being
39 * re-initialized, /etc/core-git/templates/hooks/update would
40 * cause git-init to fail here. I think this is sane but
41 * it means that the set of templates we ship by default, along
42 * with the way the namespace under .git/ is organized, should
43 * be really carefully chosen.
45 safe_create_dir(path, 1);
46 while ((de = readdir(dir)) != NULL) {
47 struct stat st_git, st_template;
48 int namelen;
49 int exists = 0;
51 if (de->d_name[0] == '.')
52 continue;
53 namelen = strlen(de->d_name);
54 if ((PATH_MAX <= baselen + namelen) ||
55 (PATH_MAX <= template_baselen + namelen))
56 die("insanely long template name %s", de->d_name);
57 memcpy(path + baselen, de->d_name, namelen+1);
58 memcpy(template + template_baselen, de->d_name, namelen+1);
59 if (lstat(path, &st_git)) {
60 if (errno != ENOENT)
61 die("cannot stat %s", path);
63 else
64 exists = 1;
66 if (lstat(template, &st_template))
67 die("cannot stat template %s", template);
69 if (S_ISDIR(st_template.st_mode)) {
70 DIR *subdir = opendir(template);
71 int baselen_sub = baselen + namelen;
72 int template_baselen_sub = template_baselen + namelen;
73 if (!subdir)
74 die("cannot opendir %s", template);
75 path[baselen_sub++] =
76 template[template_baselen_sub++] = '/';
77 path[baselen_sub] =
78 template[template_baselen_sub] = 0;
79 copy_templates_1(path, baselen_sub,
80 template, template_baselen_sub,
81 subdir);
82 closedir(subdir);
84 else if (exists)
85 continue;
86 else if (S_ISLNK(st_template.st_mode)) {
87 char lnk[256];
88 int len;
89 len = readlink(template, lnk, sizeof(lnk));
90 if (len < 0)
91 die("cannot readlink %s", template);
92 if (sizeof(lnk) <= len)
93 die("insanely long symlink %s", template);
94 lnk[len] = 0;
95 if (symlink(lnk, path))
96 die("cannot symlink %s %s", lnk, path);
98 else if (S_ISREG(st_template.st_mode)) {
99 if (copy_file(path, template, st_template.st_mode))
100 die("cannot copy %s to %s", template, path);
102 else
103 error("ignoring template %s", template);
107 static void copy_templates(const char *template_dir)
109 char path[PATH_MAX];
110 char template_path[PATH_MAX];
111 int template_len;
112 DIR *dir;
113 const char *git_dir = get_git_dir();
114 int len = strlen(git_dir);
116 if (!template_dir)
117 template_dir = getenv(TEMPLATE_DIR_ENVIRONMENT);
118 if (!template_dir)
119 template_dir = system_path(DEFAULT_GIT_TEMPLATE_DIR);
120 if (!template_dir[0])
121 return;
122 strcpy(template_path, template_dir);
123 template_len = strlen(template_path);
124 if (template_path[template_len-1] != '/') {
125 template_path[template_len++] = '/';
126 template_path[template_len] = 0;
128 dir = opendir(template_path);
129 if (!dir) {
130 fprintf(stderr, "warning: templates not found %s\n",
131 template_dir);
132 return;
135 /* Make sure that template is from the correct vintage */
136 strcpy(template_path + template_len, "config");
137 repository_format_version = 0;
138 git_config_from_file(check_repository_format_version,
139 template_path, NULL);
140 template_path[template_len] = 0;
142 if (repository_format_version &&
143 repository_format_version != GIT_REPO_VERSION) {
144 fprintf(stderr, "warning: not copying templates of "
145 "a wrong format version %d from '%s'\n",
146 repository_format_version,
147 template_dir);
148 closedir(dir);
149 return;
152 memcpy(path, git_dir, len);
153 if (len && path[len - 1] != '/')
154 path[len++] = '/';
155 path[len] = 0;
156 copy_templates_1(path, len,
157 template_path, template_len,
158 dir);
159 closedir(dir);
162 static int create_default_files(const char *template_path)
164 const char *git_dir = get_git_dir();
165 unsigned len = strlen(git_dir);
166 static char path[PATH_MAX];
167 struct stat st1;
168 char repo_version_string[10];
169 char junk[2];
170 int reinit;
171 int filemode;
173 if (len > sizeof(path)-50)
174 die("insane git directory %s", git_dir);
175 memcpy(path, git_dir, len);
177 if (len && path[len-1] != '/')
178 path[len++] = '/';
181 * Create .git/refs/{heads,tags}
183 safe_create_dir(git_path("refs"), 1);
184 safe_create_dir(git_path("refs/heads"), 1);
185 safe_create_dir(git_path("refs/tags"), 1);
187 /* First copy the templates -- we might have the default
188 * config file there, in which case we would want to read
189 * from it after installing.
191 copy_templates(template_path);
193 git_config(git_default_config, NULL);
196 * We would have created the above under user's umask -- under
197 * shared-repository settings, we would need to fix them up.
199 if (shared_repository) {
200 adjust_shared_perm(get_git_dir());
201 adjust_shared_perm(git_path("refs"));
202 adjust_shared_perm(git_path("refs/heads"));
203 adjust_shared_perm(git_path("refs/tags"));
207 * Create the default symlink from ".git/HEAD" to the "master"
208 * branch, if it does not exist yet.
210 strcpy(path + len, "HEAD");
211 reinit = (!access(path, R_OK)
212 || readlink(path, junk, sizeof(junk)-1) != -1);
213 if (!reinit) {
214 if (create_symref("HEAD", "refs/heads/master", NULL) < 0)
215 exit(1);
218 /* This forces creation of new config file */
219 sprintf(repo_version_string, "%d", GIT_REPO_VERSION);
220 git_config_set("core.repositoryformatversion", repo_version_string);
222 path[len] = 0;
223 strcpy(path + len, "config");
225 /* Check filemode trustability */
226 filemode = TEST_FILEMODE;
227 if (TEST_FILEMODE && !lstat(path, &st1)) {
228 struct stat st2;
229 filemode = (!chmod(path, st1.st_mode ^ S_IXUSR) &&
230 !lstat(path, &st2) &&
231 st1.st_mode != st2.st_mode);
233 git_config_set("core.filemode", filemode ? "true" : "false");
235 if (is_bare_repository())
236 git_config_set("core.bare", "true");
237 else {
238 const char *work_tree = get_git_work_tree();
239 git_config_set("core.bare", "false");
240 /* allow template config file to override the default */
241 if (log_all_ref_updates == -1)
242 git_config_set("core.logallrefupdates", "true");
243 if (prefixcmp(git_dir, work_tree) ||
244 strcmp(git_dir + strlen(work_tree), "/.git")) {
245 git_config_set("core.worktree", work_tree);
249 if (!reinit) {
250 /* Check if symlink is supported in the work tree */
251 path[len] = 0;
252 strcpy(path + len, "tXXXXXX");
253 if (!close(xmkstemp(path)) &&
254 !unlink(path) &&
255 !symlink("testing", path) &&
256 !lstat(path, &st1) &&
257 S_ISLNK(st1.st_mode))
258 unlink(path); /* good */
259 else
260 git_config_set("core.symlinks", "false");
262 /* Check if the filesystem is case-insensitive */
263 path[len] = 0;
264 strcpy(path + len, "CoNfIg");
265 if (!access(path, F_OK))
266 git_config_set("core.ignorecase", "true");
269 return reinit;
272 int init_db(const char *template_dir, unsigned int flags)
274 const char *sha1_dir;
275 char *path;
276 int len, reinit;
278 safe_create_dir(get_git_dir(), 0);
280 /* Check to see if the repository version is right.
281 * Note that a newly created repository does not have
282 * config file, so this will not fail. What we are catching
283 * is an attempt to reinitialize new repository with an old tool.
285 check_repository_format();
287 reinit = create_default_files(template_dir);
289 sha1_dir = get_object_directory();
290 len = strlen(sha1_dir);
291 path = xmalloc(len + 40);
292 memcpy(path, sha1_dir, len);
294 safe_create_dir(sha1_dir, 1);
295 strcpy(path+len, "/pack");
296 safe_create_dir(path, 1);
297 strcpy(path+len, "/info");
298 safe_create_dir(path, 1);
300 if (shared_repository) {
301 char buf[10];
302 /* We do not spell "group" and such, so that
303 * the configuration can be read by older version
304 * of git. Note, we use octal numbers for new share modes,
305 * and compatibility values for PERM_GROUP and
306 * PERM_EVERYBODY.
308 if (shared_repository == PERM_GROUP)
309 sprintf(buf, "%d", OLD_PERM_GROUP);
310 else if (shared_repository == PERM_EVERYBODY)
311 sprintf(buf, "%d", OLD_PERM_EVERYBODY);
312 else
313 sprintf(buf, "0%o", shared_repository);
314 git_config_set("core.sharedrepository", buf);
315 git_config_set("receive.denyNonFastforwards", "true");
318 if (!(flags & INIT_DB_QUIET))
319 printf("%s%s Git repository in %s/\n",
320 reinit ? "Reinitialized existing" : "Initialized empty",
321 shared_repository ? " shared" : "",
322 get_git_dir());
324 return 0;
327 static int guess_repository_type(const char *git_dir)
329 char cwd[PATH_MAX];
330 const char *slash;
333 * "GIT_DIR=. git init" is always bare.
334 * "GIT_DIR=`pwd` git init" too.
336 if (!strcmp(".", git_dir))
337 return 1;
338 if (!getcwd(cwd, sizeof(cwd)))
339 die("cannot tell cwd");
340 if (!strcmp(git_dir, cwd))
341 return 1;
343 * "GIT_DIR=.git or GIT_DIR=something/.git is usually not.
345 if (!strcmp(git_dir, ".git"))
346 return 0;
347 slash = strrchr(git_dir, '/');
348 if (slash && !strcmp(slash, "/.git"))
349 return 0;
352 * Otherwise it is often bare. At this point
353 * we are just guessing.
355 return 1;
358 static const char init_db_usage[] =
359 "git init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]]";
362 * If you want to, you can share the DB area with any number of branches.
363 * That has advantages: you can save space by sharing all the SHA1 objects.
364 * On the other hand, it might just make lookup slower and messier. You
365 * be the judge. The default case is to have one DB per managed directory.
367 int cmd_init_db(int argc, const char **argv, const char *prefix)
369 const char *git_dir;
370 const char *template_dir = NULL;
371 unsigned int flags = 0;
372 int i;
374 for (i = 1; i < argc; i++, argv++) {
375 const char *arg = argv[1];
376 if (!prefixcmp(arg, "--template="))
377 template_dir = arg+11;
378 else if (!strcmp(arg, "--bare")) {
379 static char git_dir[PATH_MAX+1];
380 is_bare_repository_cfg = 1;
381 setenv(GIT_DIR_ENVIRONMENT, getcwd(git_dir,
382 sizeof(git_dir)), 0);
383 } else if (!strcmp(arg, "--shared"))
384 shared_repository = PERM_GROUP;
385 else if (!prefixcmp(arg, "--shared="))
386 shared_repository = git_config_perm("arg", arg+9);
387 else if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet"))
388 flags |= INIT_DB_QUIET;
389 else
390 usage(init_db_usage);
394 * GIT_WORK_TREE makes sense only in conjunction with GIT_DIR
395 * without --bare. Catch the error early.
397 git_dir = getenv(GIT_DIR_ENVIRONMENT);
398 if ((!git_dir || is_bare_repository_cfg == 1)
399 && getenv(GIT_WORK_TREE_ENVIRONMENT))
400 die("%s (or --work-tree=<directory>) not allowed without "
401 "specifying %s (or --git-dir=<directory>)",
402 GIT_WORK_TREE_ENVIRONMENT,
403 GIT_DIR_ENVIRONMENT);
406 * Set up the default .git directory contents
408 if (!git_dir)
409 git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
411 if (is_bare_repository_cfg < 0)
412 is_bare_repository_cfg = guess_repository_type(git_dir);
414 if (!is_bare_repository_cfg) {
415 if (git_dir) {
416 const char *git_dir_parent = strrchr(git_dir, '/');
417 if (git_dir_parent) {
418 char *rel = xstrndup(git_dir, git_dir_parent - git_dir);
419 git_work_tree_cfg = xstrdup(make_absolute_path(rel));
420 free(rel);
423 if (!git_work_tree_cfg) {
424 git_work_tree_cfg = xcalloc(PATH_MAX, 1);
425 if (!getcwd(git_work_tree_cfg, PATH_MAX))
426 die ("Cannot access current working directory.");
428 if (access(get_git_work_tree(), X_OK))
429 die ("Cannot access work tree '%s'",
430 get_git_work_tree());
433 set_git_dir(make_absolute_path(git_dir));
435 return init_db(template_dir, flags);