Makefile: allow building without perl
[git/kirr.git] / builtin-init-db.c
blob4e02b33bb77b8957940562f87bc0b6faaeb3a101
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 int init_is_bare_repository = 0;
21 static int init_shared_repository = -1;
23 static void safe_create_dir(const char *dir, int share)
25 if (mkdir(dir, 0777) < 0) {
26 if (errno != EEXIST) {
27 perror(dir);
28 exit(1);
31 else if (share && adjust_shared_perm(dir))
32 die("Could not make %s writable by group", dir);
35 static void copy_templates_1(char *path, int baselen,
36 char *template, int template_baselen,
37 DIR *dir)
39 struct dirent *de;
41 /* Note: if ".git/hooks" file exists in the repository being
42 * re-initialized, /etc/core-git/templates/hooks/update would
43 * cause "git init" to fail here. I think this is sane but
44 * it means that the set of templates we ship by default, along
45 * with the way the namespace under .git/ is organized, should
46 * be really carefully chosen.
48 safe_create_dir(path, 1);
49 while ((de = readdir(dir)) != NULL) {
50 struct stat st_git, st_template;
51 int namelen;
52 int exists = 0;
54 if (de->d_name[0] == '.')
55 continue;
56 namelen = strlen(de->d_name);
57 if ((PATH_MAX <= baselen + namelen) ||
58 (PATH_MAX <= template_baselen + namelen))
59 die("insanely long template name %s", de->d_name);
60 memcpy(path + baselen, de->d_name, namelen+1);
61 memcpy(template + template_baselen, de->d_name, namelen+1);
62 if (lstat(path, &st_git)) {
63 if (errno != ENOENT)
64 die("cannot stat %s", path);
66 else
67 exists = 1;
69 if (lstat(template, &st_template))
70 die("cannot stat template %s", template);
72 if (S_ISDIR(st_template.st_mode)) {
73 DIR *subdir = opendir(template);
74 int baselen_sub = baselen + namelen;
75 int template_baselen_sub = template_baselen + namelen;
76 if (!subdir)
77 die("cannot opendir %s", template);
78 path[baselen_sub++] =
79 template[template_baselen_sub++] = '/';
80 path[baselen_sub] =
81 template[template_baselen_sub] = 0;
82 copy_templates_1(path, baselen_sub,
83 template, template_baselen_sub,
84 subdir);
85 closedir(subdir);
87 else if (exists)
88 continue;
89 else if (S_ISLNK(st_template.st_mode)) {
90 char lnk[256];
91 int len;
92 len = readlink(template, lnk, sizeof(lnk));
93 if (len < 0)
94 die("cannot readlink %s", template);
95 if (sizeof(lnk) <= len)
96 die("insanely long symlink %s", template);
97 lnk[len] = 0;
98 if (symlink(lnk, path))
99 die("cannot symlink %s %s", lnk, path);
101 else if (S_ISREG(st_template.st_mode)) {
102 if (copy_file(path, template, st_template.st_mode))
103 die("cannot copy %s to %s", template, path);
105 else
106 error("ignoring template %s", template);
110 static void copy_templates(const char *template_dir)
112 char path[PATH_MAX];
113 char template_path[PATH_MAX];
114 int template_len;
115 DIR *dir;
116 const char *git_dir = get_git_dir();
117 int len = strlen(git_dir);
119 if (!template_dir)
120 template_dir = getenv(TEMPLATE_DIR_ENVIRONMENT);
121 if (!template_dir)
122 template_dir = system_path(DEFAULT_GIT_TEMPLATE_DIR);
123 if (!template_dir[0])
124 return;
125 strcpy(template_path, template_dir);
126 template_len = strlen(template_path);
127 if (template_path[template_len-1] != '/') {
128 template_path[template_len++] = '/';
129 template_path[template_len] = 0;
131 dir = opendir(template_path);
132 if (!dir) {
133 warning("templates not found %s", template_dir);
134 return;
137 /* Make sure that template is from the correct vintage */
138 strcpy(template_path + template_len, "config");
139 repository_format_version = 0;
140 git_config_from_file(check_repository_format_version,
141 template_path, NULL);
142 template_path[template_len] = 0;
144 if (repository_format_version &&
145 repository_format_version != GIT_REPO_VERSION) {
146 warning("not copying templates of "
147 "a wrong format version %d from '%s'",
148 repository_format_version,
149 template_dir);
150 closedir(dir);
151 return;
154 memcpy(path, git_dir, len);
155 if (len && path[len - 1] != '/')
156 path[len++] = '/';
157 path[len] = 0;
158 copy_templates_1(path, len,
159 template_path, template_len,
160 dir);
161 closedir(dir);
164 static int create_default_files(const char *template_path)
166 const char *git_dir = get_git_dir();
167 unsigned len = strlen(git_dir);
168 static char path[PATH_MAX];
169 struct stat st1;
170 char repo_version_string[10];
171 char junk[2];
172 int reinit;
173 int filemode;
175 if (len > sizeof(path)-50)
176 die("insane git directory %s", git_dir);
177 memcpy(path, git_dir, len);
179 if (len && path[len-1] != '/')
180 path[len++] = '/';
183 * Create .git/refs/{heads,tags}
185 safe_create_dir(git_path("refs"), 1);
186 safe_create_dir(git_path("refs/heads"), 1);
187 safe_create_dir(git_path("refs/tags"), 1);
189 /* First copy the templates -- we might have the default
190 * config file there, in which case we would want to read
191 * from it after installing.
193 copy_templates(template_path);
195 git_config(git_default_config, NULL);
196 is_bare_repository_cfg = init_is_bare_repository;
198 /* reading existing config may have overwrote it */
199 if (init_shared_repository != -1)
200 shared_repository = init_shared_repository;
203 * We would have created the above under user's umask -- under
204 * shared-repository settings, we would need to fix them up.
206 if (shared_repository) {
207 adjust_shared_perm(get_git_dir());
208 adjust_shared_perm(git_path("refs"));
209 adjust_shared_perm(git_path("refs/heads"));
210 adjust_shared_perm(git_path("refs/tags"));
214 * Create the default symlink from ".git/HEAD" to the "master"
215 * branch, if it does not exist yet.
217 strcpy(path + len, "HEAD");
218 reinit = (!access(path, R_OK)
219 || readlink(path, junk, sizeof(junk)-1) != -1);
220 if (!reinit) {
221 if (create_symref("HEAD", "refs/heads/master", NULL) < 0)
222 exit(1);
225 /* This forces creation of new config file */
226 sprintf(repo_version_string, "%d", GIT_REPO_VERSION);
227 git_config_set("core.repositoryformatversion", repo_version_string);
229 path[len] = 0;
230 strcpy(path + len, "config");
232 /* Check filemode trustability */
233 filemode = TEST_FILEMODE;
234 if (TEST_FILEMODE && !lstat(path, &st1)) {
235 struct stat st2;
236 filemode = (!chmod(path, st1.st_mode ^ S_IXUSR) &&
237 !lstat(path, &st2) &&
238 st1.st_mode != st2.st_mode);
240 git_config_set("core.filemode", filemode ? "true" : "false");
242 if (is_bare_repository())
243 git_config_set("core.bare", "true");
244 else {
245 const char *work_tree = get_git_work_tree();
246 git_config_set("core.bare", "false");
247 /* allow template config file to override the default */
248 if (log_all_ref_updates == -1)
249 git_config_set("core.logallrefupdates", "true");
250 if (prefixcmp(git_dir, work_tree) ||
251 strcmp(git_dir + strlen(work_tree), "/.git")) {
252 git_config_set("core.worktree", work_tree);
256 if (!reinit) {
257 /* Check if symlink is supported in the work tree */
258 path[len] = 0;
259 strcpy(path + len, "tXXXXXX");
260 if (!close(xmkstemp(path)) &&
261 !unlink(path) &&
262 !symlink("testing", path) &&
263 !lstat(path, &st1) &&
264 S_ISLNK(st1.st_mode))
265 unlink(path); /* good */
266 else
267 git_config_set("core.symlinks", "false");
269 /* Check if the filesystem is case-insensitive */
270 path[len] = 0;
271 strcpy(path + len, "CoNfIg");
272 if (!access(path, F_OK))
273 git_config_set("core.ignorecase", "true");
276 return reinit;
279 int init_db(const char *template_dir, unsigned int flags)
281 const char *sha1_dir;
282 char *path;
283 int len, reinit;
285 safe_create_dir(get_git_dir(), 0);
287 init_is_bare_repository = is_bare_repository();
289 /* Check to see if the repository version is right.
290 * Note that a newly created repository does not have
291 * config file, so this will not fail. What we are catching
292 * is an attempt to reinitialize new repository with an old tool.
294 check_repository_format();
296 reinit = create_default_files(template_dir);
298 sha1_dir = get_object_directory();
299 len = strlen(sha1_dir);
300 path = xmalloc(len + 40);
301 memcpy(path, sha1_dir, len);
303 safe_create_dir(sha1_dir, 1);
304 strcpy(path+len, "/pack");
305 safe_create_dir(path, 1);
306 strcpy(path+len, "/info");
307 safe_create_dir(path, 1);
309 if (shared_repository) {
310 char buf[10];
311 /* We do not spell "group" and such, so that
312 * the configuration can be read by older version
313 * of git. Note, we use octal numbers for new share modes,
314 * and compatibility values for PERM_GROUP and
315 * PERM_EVERYBODY.
317 if (shared_repository < 0)
318 /* force to the mode value */
319 sprintf(buf, "0%o", -shared_repository);
320 else if (shared_repository == PERM_GROUP)
321 sprintf(buf, "%d", OLD_PERM_GROUP);
322 else if (shared_repository == PERM_EVERYBODY)
323 sprintf(buf, "%d", OLD_PERM_EVERYBODY);
324 else
325 die("oops");
326 git_config_set("core.sharedrepository", buf);
327 git_config_set("receive.denyNonFastforwards", "true");
330 if (!(flags & INIT_DB_QUIET))
331 printf("%s%s Git repository in %s/\n",
332 reinit ? "Reinitialized existing" : "Initialized empty",
333 shared_repository ? " shared" : "",
334 get_git_dir());
336 return 0;
339 static int guess_repository_type(const char *git_dir)
341 char cwd[PATH_MAX];
342 const char *slash;
345 * "GIT_DIR=. git init" is always bare.
346 * "GIT_DIR=`pwd` git init" too.
348 if (!strcmp(".", git_dir))
349 return 1;
350 if (!getcwd(cwd, sizeof(cwd)))
351 die("cannot tell cwd");
352 if (!strcmp(git_dir, cwd))
353 return 1;
355 * "GIT_DIR=.git or GIT_DIR=something/.git is usually not.
357 if (!strcmp(git_dir, ".git"))
358 return 0;
359 slash = strrchr(git_dir, '/');
360 if (slash && !strcmp(slash, "/.git"))
361 return 0;
364 * Otherwise it is often bare. At this point
365 * we are just guessing.
367 return 1;
370 static const char init_db_usage[] =
371 "git init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]]";
374 * If you want to, you can share the DB area with any number of branches.
375 * That has advantages: you can save space by sharing all the SHA1 objects.
376 * On the other hand, it might just make lookup slower and messier. You
377 * be the judge. The default case is to have one DB per managed directory.
379 int cmd_init_db(int argc, const char **argv, const char *prefix)
381 const char *git_dir;
382 const char *template_dir = NULL;
383 unsigned int flags = 0;
384 int i;
386 for (i = 1; i < argc; i++, argv++) {
387 const char *arg = argv[1];
388 if (!prefixcmp(arg, "--template="))
389 template_dir = arg+11;
390 else if (!strcmp(arg, "--bare")) {
391 static char git_dir[PATH_MAX+1];
392 is_bare_repository_cfg = 1;
393 setenv(GIT_DIR_ENVIRONMENT, getcwd(git_dir,
394 sizeof(git_dir)), 0);
395 } else if (!strcmp(arg, "--shared"))
396 init_shared_repository = PERM_GROUP;
397 else if (!prefixcmp(arg, "--shared="))
398 init_shared_repository = git_config_perm("arg", arg+9);
399 else if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet"))
400 flags |= INIT_DB_QUIET;
401 else
402 usage(init_db_usage);
405 if (init_shared_repository != -1)
406 shared_repository = init_shared_repository;
409 * GIT_WORK_TREE makes sense only in conjunction with GIT_DIR
410 * without --bare. Catch the error early.
412 git_dir = getenv(GIT_DIR_ENVIRONMENT);
413 if ((!git_dir || is_bare_repository_cfg == 1)
414 && getenv(GIT_WORK_TREE_ENVIRONMENT))
415 die("%s (or --work-tree=<directory>) not allowed without "
416 "specifying %s (or --git-dir=<directory>)",
417 GIT_WORK_TREE_ENVIRONMENT,
418 GIT_DIR_ENVIRONMENT);
421 * Set up the default .git directory contents
423 if (!git_dir)
424 git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
426 if (is_bare_repository_cfg < 0)
427 is_bare_repository_cfg = guess_repository_type(git_dir);
429 if (!is_bare_repository_cfg) {
430 if (git_dir) {
431 const char *git_dir_parent = strrchr(git_dir, '/');
432 if (git_dir_parent) {
433 char *rel = xstrndup(git_dir, git_dir_parent - git_dir);
434 git_work_tree_cfg = xstrdup(make_absolute_path(rel));
435 free(rel);
438 if (!git_work_tree_cfg) {
439 git_work_tree_cfg = xcalloc(PATH_MAX, 1);
440 if (!getcwd(git_work_tree_cfg, PATH_MAX))
441 die ("Cannot access current working directory.");
443 if (access(get_git_work_tree(), X_OK))
444 die ("Cannot access work tree '%s'",
445 get_git_work_tree());
448 set_git_dir(make_absolute_path(git_dir));
450 return init_db(template_dir, flags);