patch-ids: use the new generic "sha1_pos" function to lookup sha1
[git/kusma.git] / builtin-init-db.c
blobfc63d0fce5d3067bec884ac3ccaf12e0fd0d4468
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;
197 if (init_shared_repository != -1)
198 shared_repository = init_shared_repository;
201 * We would have created the above under user's umask -- under
202 * shared-repository settings, we would need to fix them up.
204 if (shared_repository) {
205 adjust_shared_perm(get_git_dir());
206 adjust_shared_perm(git_path("refs"));
207 adjust_shared_perm(git_path("refs/heads"));
208 adjust_shared_perm(git_path("refs/tags"));
212 * Create the default symlink from ".git/HEAD" to the "master"
213 * branch, if it does not exist yet.
215 strcpy(path + len, "HEAD");
216 reinit = (!access(path, R_OK)
217 || readlink(path, junk, sizeof(junk)-1) != -1);
218 if (!reinit) {
219 if (create_symref("HEAD", "refs/heads/master", NULL) < 0)
220 exit(1);
223 /* This forces creation of new config file */
224 sprintf(repo_version_string, "%d", GIT_REPO_VERSION);
225 git_config_set("core.repositoryformatversion", repo_version_string);
227 path[len] = 0;
228 strcpy(path + len, "config");
230 /* Check filemode trustability */
231 filemode = TEST_FILEMODE;
232 if (TEST_FILEMODE && !lstat(path, &st1)) {
233 struct stat st2;
234 filemode = (!chmod(path, st1.st_mode ^ S_IXUSR) &&
235 !lstat(path, &st2) &&
236 st1.st_mode != st2.st_mode);
238 git_config_set("core.filemode", filemode ? "true" : "false");
240 if (is_bare_repository())
241 git_config_set("core.bare", "true");
242 else {
243 const char *work_tree = get_git_work_tree();
244 git_config_set("core.bare", "false");
245 /* allow template config file to override the default */
246 if (log_all_ref_updates == -1)
247 git_config_set("core.logallrefupdates", "true");
248 if (prefixcmp(git_dir, work_tree) ||
249 strcmp(git_dir + strlen(work_tree), "/.git")) {
250 git_config_set("core.worktree", work_tree);
254 if (!reinit) {
255 /* Check if symlink is supported in the work tree */
256 path[len] = 0;
257 strcpy(path + len, "tXXXXXX");
258 if (!close(xmkstemp(path)) &&
259 !unlink(path) &&
260 !symlink("testing", path) &&
261 !lstat(path, &st1) &&
262 S_ISLNK(st1.st_mode))
263 unlink(path); /* good */
264 else
265 git_config_set("core.symlinks", "false");
267 /* Check if the filesystem is case-insensitive */
268 path[len] = 0;
269 strcpy(path + len, "CoNfIg");
270 if (!access(path, F_OK))
271 git_config_set("core.ignorecase", "true");
274 return reinit;
277 int init_db(const char *template_dir, unsigned int flags)
279 const char *sha1_dir;
280 char *path;
281 int len, reinit;
283 safe_create_dir(get_git_dir(), 0);
285 init_is_bare_repository = is_bare_repository();
287 /* Check to see if the repository version is right.
288 * Note that a newly created repository does not have
289 * config file, so this will not fail. What we are catching
290 * is an attempt to reinitialize new repository with an old tool.
292 check_repository_format();
294 reinit = create_default_files(template_dir);
296 sha1_dir = get_object_directory();
297 len = strlen(sha1_dir);
298 path = xmalloc(len + 40);
299 memcpy(path, sha1_dir, len);
301 safe_create_dir(sha1_dir, 1);
302 strcpy(path+len, "/pack");
303 safe_create_dir(path, 1);
304 strcpy(path+len, "/info");
305 safe_create_dir(path, 1);
307 if (shared_repository) {
308 char buf[10];
309 /* We do not spell "group" and such, so that
310 * the configuration can be read by older version
311 * of git. Note, we use octal numbers for new share modes,
312 * and compatibility values for PERM_GROUP and
313 * PERM_EVERYBODY.
315 if (shared_repository == PERM_GROUP)
316 sprintf(buf, "%d", OLD_PERM_GROUP);
317 else if (shared_repository == PERM_EVERYBODY)
318 sprintf(buf, "%d", OLD_PERM_EVERYBODY);
319 else
320 sprintf(buf, "0%o", shared_repository);
321 git_config_set("core.sharedrepository", buf);
322 git_config_set("receive.denyNonFastforwards", "true");
325 if (!(flags & INIT_DB_QUIET))
326 printf("%s%s Git repository in %s/\n",
327 reinit ? "Reinitialized existing" : "Initialized empty",
328 shared_repository ? " shared" : "",
329 get_git_dir());
331 return 0;
334 static int guess_repository_type(const char *git_dir)
336 char cwd[PATH_MAX];
337 const char *slash;
340 * "GIT_DIR=. git init" is always bare.
341 * "GIT_DIR=`pwd` git init" too.
343 if (!strcmp(".", git_dir))
344 return 1;
345 if (!getcwd(cwd, sizeof(cwd)))
346 die("cannot tell cwd");
347 if (!strcmp(git_dir, cwd))
348 return 1;
350 * "GIT_DIR=.git or GIT_DIR=something/.git is usually not.
352 if (!strcmp(git_dir, ".git"))
353 return 0;
354 slash = strrchr(git_dir, '/');
355 if (slash && !strcmp(slash, "/.git"))
356 return 0;
359 * Otherwise it is often bare. At this point
360 * we are just guessing.
362 return 1;
365 static const char init_db_usage[] =
366 "git init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]]";
369 * If you want to, you can share the DB area with any number of branches.
370 * That has advantages: you can save space by sharing all the SHA1 objects.
371 * On the other hand, it might just make lookup slower and messier. You
372 * be the judge. The default case is to have one DB per managed directory.
374 int cmd_init_db(int argc, const char **argv, const char *prefix)
376 const char *git_dir;
377 const char *template_dir = NULL;
378 unsigned int flags = 0;
379 int i;
381 for (i = 1; i < argc; i++, argv++) {
382 const char *arg = argv[1];
383 if (!prefixcmp(arg, "--template="))
384 template_dir = arg+11;
385 else if (!strcmp(arg, "--bare")) {
386 static char git_dir[PATH_MAX+1];
387 is_bare_repository_cfg = 1;
388 setenv(GIT_DIR_ENVIRONMENT, getcwd(git_dir,
389 sizeof(git_dir)), 0);
390 } else if (!strcmp(arg, "--shared"))
391 init_shared_repository = PERM_GROUP;
392 else if (!prefixcmp(arg, "--shared="))
393 init_shared_repository = git_config_perm("arg", arg+9);
394 else if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet"))
395 flags |= INIT_DB_QUIET;
396 else
397 usage(init_db_usage);
401 * GIT_WORK_TREE makes sense only in conjunction with GIT_DIR
402 * without --bare. Catch the error early.
404 git_dir = getenv(GIT_DIR_ENVIRONMENT);
405 if ((!git_dir || is_bare_repository_cfg == 1)
406 && getenv(GIT_WORK_TREE_ENVIRONMENT))
407 die("%s (or --work-tree=<directory>) not allowed without "
408 "specifying %s (or --git-dir=<directory>)",
409 GIT_WORK_TREE_ENVIRONMENT,
410 GIT_DIR_ENVIRONMENT);
413 * Set up the default .git directory contents
415 if (!git_dir)
416 git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
418 if (is_bare_repository_cfg < 0)
419 is_bare_repository_cfg = guess_repository_type(git_dir);
421 if (!is_bare_repository_cfg) {
422 if (git_dir) {
423 const char *git_dir_parent = strrchr(git_dir, '/');
424 if (git_dir_parent) {
425 char *rel = xstrndup(git_dir, git_dir_parent - git_dir);
426 git_work_tree_cfg = xstrdup(make_absolute_path(rel));
427 free(rel);
430 if (!git_work_tree_cfg) {
431 git_work_tree_cfg = xcalloc(PATH_MAX, 1);
432 if (!getcwd(git_work_tree_cfg, PATH_MAX))
433 die ("Cannot access current working directory.");
435 if (access(get_git_work_tree(), X_OK))
436 die ("Cannot access work tree '%s'",
437 get_git_work_tree());
440 set_git_dir(make_absolute_path(git_dir));
442 return init_db(template_dir, flags);