Add zlib
[git/pclouds.git] / builtin-init-db.c
blob62e579d42a91ee8e5152a383bcb6cd5de0e8343b
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 int copy_file(const char *dst, const char *src, int mode)
34 int fdi, fdo, status;
36 mode = (mode & 0111) ? 0777 : 0666;
37 if ((fdi = open(src, O_RDONLY)) < 0)
38 return fdi;
39 if ((fdo = open(dst, O_WRONLY | O_CREAT | O_EXCL, mode)) < 0) {
40 close(fdi);
41 return fdo;
43 status = copy_fd(fdi, fdo);
44 if (close(fdo) != 0)
45 return error("%s: write error: %s", dst, strerror(errno));
47 if (!status && adjust_shared_perm(dst))
48 return -1;
50 return status;
53 static void copy_templates_1(char *path, int baselen,
54 char *template, int template_baselen,
55 DIR *dir)
57 struct dirent *de;
59 /* Note: if ".git/hooks" file exists in the repository being
60 * re-initialized, /etc/core-git/templates/hooks/update would
61 * cause git-init to fail here. I think this is sane but
62 * it means that the set of templates we ship by default, along
63 * with the way the namespace under .git/ is organized, should
64 * be really carefully chosen.
66 safe_create_dir(path, 1);
67 while ((de = readdir(dir)) != NULL) {
68 struct stat st_git, st_template;
69 int namelen;
70 int exists = 0;
72 if (de->d_name[0] == '.')
73 continue;
74 namelen = strlen(de->d_name);
75 if ((PATH_MAX <= baselen + namelen) ||
76 (PATH_MAX <= template_baselen + namelen))
77 die("insanely long template name %s", de->d_name);
78 memcpy(path + baselen, de->d_name, namelen+1);
79 memcpy(template + template_baselen, de->d_name, namelen+1);
80 if (lstat(path, &st_git)) {
81 if (errno != ENOENT)
82 die("cannot stat %s", path);
84 else
85 exists = 1;
87 if (lstat(template, &st_template))
88 die("cannot stat template %s", template);
90 if (S_ISDIR(st_template.st_mode)) {
91 DIR *subdir = opendir(template);
92 int baselen_sub = baselen + namelen;
93 int template_baselen_sub = template_baselen + namelen;
94 if (!subdir)
95 die("cannot opendir %s", template);
96 path[baselen_sub++] =
97 template[template_baselen_sub++] = '/';
98 path[baselen_sub] =
99 template[template_baselen_sub] = 0;
100 copy_templates_1(path, baselen_sub,
101 template, template_baselen_sub,
102 subdir);
103 closedir(subdir);
105 else if (exists)
106 continue;
107 else if (S_ISLNK(st_template.st_mode)) {
108 char lnk[256];
109 int len;
110 len = readlink(template, lnk, sizeof(lnk));
111 if (len < 0)
112 die("cannot readlink %s", template);
113 if (sizeof(lnk) <= len)
114 die("insanely long symlink %s", template);
115 lnk[len] = 0;
116 if (symlink(lnk, path))
117 die("cannot symlink %s %s", lnk, path);
119 else if (S_ISREG(st_template.st_mode)) {
120 if (copy_file(path, template, st_template.st_mode))
121 die("cannot copy %s to %s", template, path);
123 else
124 error("ignoring template %s", template);
128 static void copy_templates(const char *git_dir, int len, const char *template_dir)
130 char path[PATH_MAX];
131 char template_path[PATH_MAX];
132 int template_len;
133 DIR *dir;
135 if (!template_dir)
136 template_dir = getenv(TEMPLATE_DIR_ENVIRONMENT);
137 if (!template_dir) {
139 * if the hard-coded template is relative, it is
140 * interpreted relative to the exec_dir
142 template_dir = DEFAULT_GIT_TEMPLATE_DIR;
143 if (template_dir[0] != '/' && template_dir[1] != ':') {
144 const char *exec_path = git_exec_path();
145 template_dir = prefix_path(exec_path, strlen(exec_path),
146 template_dir);
149 strcpy(template_path, template_dir);
150 template_len = strlen(template_path);
151 if (template_path[template_len-1] != '/') {
152 template_path[template_len++] = '/';
153 template_path[template_len] = 0;
155 dir = opendir(template_path);
156 if (!dir) {
157 fprintf(stderr, "warning: templates not found %s\n",
158 template_dir);
159 return;
162 /* Make sure that template is from the correct vintage */
163 strcpy(template_path + template_len, "config");
164 repository_format_version = 0;
165 git_config_from_file(check_repository_format_version,
166 template_path);
167 template_path[template_len] = 0;
169 if (repository_format_version &&
170 repository_format_version != GIT_REPO_VERSION) {
171 fprintf(stderr, "warning: not copying templates of "
172 "a wrong format version %d from '%s'\n",
173 repository_format_version,
174 template_dir);
175 closedir(dir);
176 return;
179 memcpy(path, git_dir, len);
180 path[len] = 0;
181 copy_templates_1(path, len,
182 template_path, template_len,
183 dir);
184 closedir(dir);
188 * Get the full path to the working tree specified in $GIT_WORK_TREE
189 * or NULL if no working tree is specified.
191 static const char *get_work_tree(void)
193 const char *git_work_tree;
194 char cwd[PATH_MAX];
195 static char worktree[PATH_MAX];
197 git_work_tree = getenv(GIT_WORK_TREE_ENVIRONMENT);
198 if (!git_work_tree)
199 return NULL;
200 if (!getcwd(cwd, sizeof(cwd)))
201 die("Unable to read current working directory");
202 if (chdir(git_work_tree))
203 die("Cannot change directory to specified working tree '%s'",
204 git_work_tree);
205 if (git_work_tree[0] != '/') {
206 if (!getcwd(worktree, sizeof(worktree)))
207 die("Unable to read current working directory");
208 git_work_tree = worktree;
210 if (chdir(cwd))
211 die("Cannot come back to cwd");
212 return git_work_tree;
215 static int create_default_files(const char *git_dir, const char *git_work_tree,
216 const char *template_path)
218 unsigned len = strlen(git_dir);
219 static char path[PATH_MAX];
220 unsigned char sha1[20];
221 struct stat st1;
222 char repo_version_string[10];
223 int reinit;
224 int filemode;
226 if (len > sizeof(path)-50)
227 die("insane git directory %s", git_dir);
228 memcpy(path, git_dir, len);
230 if (len && path[len-1] != '/')
231 path[len++] = '/';
234 * Create .git/refs/{heads,tags}
236 strcpy(path + len, "refs");
237 safe_create_dir(path, 1);
238 strcpy(path + len, "refs/heads");
239 safe_create_dir(path, 1);
240 strcpy(path + len, "refs/tags");
241 safe_create_dir(path, 1);
243 /* First copy the templates -- we might have the default
244 * config file there, in which case we would want to read
245 * from it after installing.
247 path[len] = 0;
248 copy_templates(path, len, template_path);
250 git_config(git_default_config);
253 * We would have created the above under user's umask -- under
254 * shared-repository settings, we would need to fix them up.
256 if (shared_repository) {
257 path[len] = 0;
258 adjust_shared_perm(path);
259 strcpy(path + len, "refs");
260 adjust_shared_perm(path);
261 strcpy(path + len, "refs/heads");
262 adjust_shared_perm(path);
263 strcpy(path + len, "refs/tags");
264 adjust_shared_perm(path);
268 * Create the default symlink from ".git/HEAD" to the "master"
269 * branch, if it does not exist yet.
271 strcpy(path + len, "HEAD");
272 reinit = !read_ref("HEAD", sha1);
273 if (!reinit) {
274 if (create_symref("HEAD", "refs/heads/master", NULL) < 0)
275 exit(1);
278 /* This forces creation of new config file */
279 sprintf(repo_version_string, "%d", GIT_REPO_VERSION);
280 git_config_set("core.repositoryformatversion", repo_version_string);
282 path[len] = 0;
283 strcpy(path + len, "config");
285 /* Check filemode trustability */
286 filemode = TEST_FILEMODE;
287 if (TEST_FILEMODE && !lstat(path, &st1)) {
288 struct stat st2;
289 filemode = (!chmod(path, st1.st_mode ^ S_IXUSR) &&
290 !lstat(path, &st2) &&
291 st1.st_mode != st2.st_mode);
293 git_config_set("core.filemode", filemode ? "true" : "false");
295 if (is_bare_repository() && !git_work_tree) {
296 git_config_set("core.bare", "true");
298 else {
299 git_config_set("core.bare", "false");
300 /* allow template config file to override the default */
301 if (log_all_ref_updates == -1)
302 git_config_set("core.logallrefupdates", "true");
303 if (git_work_tree)
304 git_config_set("core.worktree", git_work_tree);
306 return reinit;
309 static const char init_db_usage[] =
310 "git-init [-q | --quiet] [--template=<template-directory>] [--shared]";
313 * If you want to, you can share the DB area with any number of branches.
314 * That has advantages: you can save space by sharing all the SHA1 objects.
315 * On the other hand, it might just make lookup slower and messier. You
316 * be the judge. The default case is to have one DB per managed directory.
318 int cmd_init_db(int argc, const char **argv, const char *prefix)
320 const char *git_dir;
321 const char *git_work_tree;
322 const char *sha1_dir;
323 const char *template_dir = NULL;
324 char *path;
325 int len, i, reinit;
326 int quiet = 0;
328 for (i = 1; i < argc; i++, argv++) {
329 const char *arg = argv[1];
330 if (!prefixcmp(arg, "--template="))
331 template_dir = arg+11;
332 else if (!strcmp(arg, "--shared"))
333 shared_repository = PERM_GROUP;
334 else if (!prefixcmp(arg, "--shared="))
335 shared_repository = git_config_perm("arg", arg+9);
336 else if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet"))
337 quiet = 1;
338 else
339 usage(init_db_usage);
342 git_work_tree = get_work_tree();
345 * Set up the default .git directory contents
347 git_dir = get_git_dir();
348 safe_create_dir(git_dir, 0);
350 /* Check to see if the repository version is right.
351 * Note that a newly created repository does not have
352 * config file, so this will not fail. What we are catching
353 * is an attempt to reinitialize new repository with an old tool.
355 check_repository_format();
357 reinit = create_default_files(git_dir, git_work_tree, template_dir);
360 * And set up the object store.
362 sha1_dir = get_object_directory();
363 len = strlen(sha1_dir);
364 path = xmalloc(len + 40);
365 memcpy(path, sha1_dir, len);
367 safe_create_dir(sha1_dir, 1);
368 strcpy(path+len, "/pack");
369 safe_create_dir(path, 1);
370 strcpy(path+len, "/info");
371 safe_create_dir(path, 1);
373 if (shared_repository) {
374 char buf[10];
375 /* We do not spell "group" and such, so that
376 * the configuration can be read by older version
377 * of git.
379 sprintf(buf, "%d", shared_repository);
380 git_config_set("core.sharedrepository", buf);
381 git_config_set("receive.denyNonFastforwards", "true");
384 if (!quiet)
385 printf("%s%s Git repository in %s/\n",
386 reinit ? "Reinitialized existing" : "Initialized empty",
387 shared_repository ? " shared" : "",
388 git_dir);
390 return 0;