Temporary fix for stack smashing in mailinfo
[git/spearce.git] / builtin-init-db.c
blobaf15cb27392dae6b16cee960908295ac252fe920
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "cache.h"
7 #include "builtin.h"
9 #ifndef DEFAULT_GIT_TEMPLATE_DIR
10 #define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates"
11 #endif
13 #ifdef NO_TRUSTABLE_FILEMODE
14 #define TEST_FILEMODE 0
15 #else
16 #define TEST_FILEMODE 1
17 #endif
19 static void safe_create_dir(const char *dir, int share)
21 if (mkdir(dir, 0777) < 0) {
22 if (errno != EEXIST) {
23 perror(dir);
24 exit(1);
27 else if (share && adjust_shared_perm(dir))
28 die("Could not make %s writable by group\n", dir);
31 static int copy_file(const char *dst, const char *src, int mode)
33 int fdi, fdo, status;
35 mode = (mode & 0111) ? 0777 : 0666;
36 if ((fdi = open(src, O_RDONLY)) < 0)
37 return fdi;
38 if ((fdo = open(dst, O_WRONLY | O_CREAT | O_EXCL, mode)) < 0) {
39 close(fdi);
40 return fdo;
42 status = copy_fd(fdi, fdo);
43 if (close(fdo) != 0)
44 return error("%s: write error: %s", dst, strerror(errno));
46 if (!status && adjust_shared_perm(dst))
47 return -1;
49 return status;
52 static void copy_templates_1(char *path, int baselen,
53 char *template, int template_baselen,
54 DIR *dir)
56 struct dirent *de;
58 /* Note: if ".git/hooks" file exists in the repository being
59 * re-initialized, /etc/core-git/templates/hooks/update would
60 * cause git-init to fail here. I think this is sane but
61 * it means that the set of templates we ship by default, along
62 * with the way the namespace under .git/ is organized, should
63 * be really carefully chosen.
65 safe_create_dir(path, 1);
66 while ((de = readdir(dir)) != NULL) {
67 struct stat st_git, st_template;
68 int namelen;
69 int exists = 0;
71 if (de->d_name[0] == '.')
72 continue;
73 namelen = strlen(de->d_name);
74 if ((PATH_MAX <= baselen + namelen) ||
75 (PATH_MAX <= template_baselen + namelen))
76 die("insanely long template name %s", de->d_name);
77 memcpy(path + baselen, de->d_name, namelen+1);
78 memcpy(template + template_baselen, de->d_name, namelen+1);
79 if (lstat(path, &st_git)) {
80 if (errno != ENOENT)
81 die("cannot stat %s", path);
83 else
84 exists = 1;
86 if (lstat(template, &st_template))
87 die("cannot stat template %s", template);
89 if (S_ISDIR(st_template.st_mode)) {
90 DIR *subdir = opendir(template);
91 int baselen_sub = baselen + namelen;
92 int template_baselen_sub = template_baselen + namelen;
93 if (!subdir)
94 die("cannot opendir %s", template);
95 path[baselen_sub++] =
96 template[template_baselen_sub++] = '/';
97 path[baselen_sub] =
98 template[template_baselen_sub] = 0;
99 copy_templates_1(path, baselen_sub,
100 template, template_baselen_sub,
101 subdir);
102 closedir(subdir);
104 else if (exists)
105 continue;
106 else if (S_ISLNK(st_template.st_mode)) {
107 char lnk[256];
108 int len;
109 len = readlink(template, lnk, sizeof(lnk));
110 if (len < 0)
111 die("cannot readlink %s", template);
112 if (sizeof(lnk) <= len)
113 die("insanely long symlink %s", template);
114 lnk[len] = 0;
115 if (symlink(lnk, path))
116 die("cannot symlink %s %s", lnk, path);
118 else if (S_ISREG(st_template.st_mode)) {
119 if (copy_file(path, template, st_template.st_mode))
120 die("cannot copy %s to %s", template, path);
122 else
123 error("ignoring template %s", template);
127 static void copy_templates(const char *git_dir, int len, const char *template_dir)
129 char path[PATH_MAX];
130 char template_path[PATH_MAX];
131 int template_len;
132 DIR *dir;
134 if (!template_dir) {
135 template_dir = getenv(TEMPLATE_DIR_ENVIRONMENT);
136 if (!template_dir)
137 template_dir = DEFAULT_GIT_TEMPLATE_DIR;
139 strcpy(template_path, template_dir);
140 template_len = strlen(template_path);
141 if (template_path[template_len-1] != '/') {
142 template_path[template_len++] = '/';
143 template_path[template_len] = 0;
145 dir = opendir(template_path);
146 if (!dir) {
147 fprintf(stderr, "warning: templates not found %s\n",
148 template_dir);
149 return;
152 /* Make sure that template is from the correct vintage */
153 strcpy(template_path + template_len, "config");
154 repository_format_version = 0;
155 git_config_from_file(check_repository_format_version,
156 template_path);
157 template_path[template_len] = 0;
159 if (repository_format_version &&
160 repository_format_version != GIT_REPO_VERSION) {
161 fprintf(stderr, "warning: not copying templates of "
162 "a wrong format version %d from '%s'\n",
163 repository_format_version,
164 template_dir);
165 closedir(dir);
166 return;
169 memcpy(path, git_dir, len);
170 path[len] = 0;
171 copy_templates_1(path, len,
172 template_path, template_len,
173 dir);
174 closedir(dir);
177 static int create_default_files(const char *git_dir, const char *template_path)
179 unsigned len = strlen(git_dir);
180 static char path[PATH_MAX];
181 unsigned char sha1[20];
182 struct stat st1;
183 char repo_version_string[10];
184 int reinit;
185 int filemode;
187 if (len > sizeof(path)-50)
188 die("insane git directory %s", git_dir);
189 memcpy(path, git_dir, len);
191 if (len && path[len-1] != '/')
192 path[len++] = '/';
195 * Create .git/refs/{heads,tags}
197 strcpy(path + len, "refs");
198 safe_create_dir(path, 1);
199 strcpy(path + len, "refs/heads");
200 safe_create_dir(path, 1);
201 strcpy(path + len, "refs/tags");
202 safe_create_dir(path, 1);
204 /* First copy the templates -- we might have the default
205 * config file there, in which case we would want to read
206 * from it after installing.
208 path[len] = 0;
209 copy_templates(path, len, template_path);
211 git_config(git_default_config);
214 * We would have created the above under user's umask -- under
215 * shared-repository settings, we would need to fix them up.
217 if (shared_repository) {
218 path[len] = 0;
219 adjust_shared_perm(path);
220 strcpy(path + len, "refs");
221 adjust_shared_perm(path);
222 strcpy(path + len, "refs/heads");
223 adjust_shared_perm(path);
224 strcpy(path + len, "refs/tags");
225 adjust_shared_perm(path);
229 * Create the default symlink from ".git/HEAD" to the "master"
230 * branch, if it does not exist yet.
232 strcpy(path + len, "HEAD");
233 reinit = !read_ref("HEAD", sha1);
234 if (!reinit) {
235 if (create_symref("HEAD", "refs/heads/master", NULL) < 0)
236 exit(1);
239 /* This forces creation of new config file */
240 sprintf(repo_version_string, "%d", GIT_REPO_VERSION);
241 git_config_set("core.repositoryformatversion", repo_version_string);
243 path[len] = 0;
244 strcpy(path + len, "config");
246 /* Check filemode trustability */
247 filemode = TEST_FILEMODE;
248 if (TEST_FILEMODE && !lstat(path, &st1)) {
249 struct stat st2;
250 filemode = (!chmod(path, st1.st_mode ^ S_IXUSR) &&
251 !lstat(path, &st2) &&
252 st1.st_mode != st2.st_mode);
254 git_config_set("core.filemode", filemode ? "true" : "false");
256 if (is_bare_repository())
257 git_config_set("core.bare", "true");
258 else {
259 const char *work_tree = get_git_work_tree();
260 git_config_set("core.bare", "false");
261 /* allow template config file to override the default */
262 if (log_all_ref_updates == -1)
263 git_config_set("core.logallrefupdates", "true");
264 if (work_tree != git_work_tree_cfg)
265 git_config_set("core.worktree", work_tree);
267 return reinit;
270 static void guess_repository_type(const char *git_dir)
272 char cwd[PATH_MAX];
273 const char *slash;
275 if (0 <= is_bare_repository_cfg)
276 return;
277 if (!git_dir)
278 return;
281 * "GIT_DIR=. git init" is always bare.
282 * "GIT_DIR=`pwd` git init" too.
284 if (!strcmp(".", git_dir))
285 goto force_bare;
286 if (!getcwd(cwd, sizeof(cwd)))
287 die("cannot tell cwd");
288 if (!strcmp(git_dir, cwd))
289 goto force_bare;
291 * "GIT_DIR=.git or GIT_DIR=something/.git is usually not.
293 if (!strcmp(git_dir, ".git"))
294 return;
295 slash = strrchr(git_dir, '/');
296 if (slash && !strcmp(slash, "/.git"))
297 return;
300 * Otherwise it is often bare. At this point
301 * we are just guessing.
303 force_bare:
304 is_bare_repository_cfg = 1;
305 return;
308 static const char init_db_usage[] =
309 "git-init [-q | --quiet] [--template=<template-directory>] [--shared]";
312 * If you want to, you can share the DB area with any number of branches.
313 * That has advantages: you can save space by sharing all the SHA1 objects.
314 * On the other hand, it might just make lookup slower and messier. You
315 * be the judge. The default case is to have one DB per managed directory.
317 int cmd_init_db(int argc, const char **argv, const char *prefix)
319 const char *git_dir;
320 const char *sha1_dir;
321 const char *template_dir = NULL;
322 char *path;
323 int len, i, reinit;
324 int quiet = 0;
326 for (i = 1; i < argc; i++, argv++) {
327 const char *arg = argv[1];
328 if (!prefixcmp(arg, "--template="))
329 template_dir = arg+11;
330 else if (!strcmp(arg, "--shared"))
331 shared_repository = PERM_GROUP;
332 else if (!prefixcmp(arg, "--shared="))
333 shared_repository = git_config_perm("arg", arg+9);
334 else if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet"))
335 quiet = 1;
336 else
337 usage(init_db_usage);
341 * GIT_WORK_TREE makes sense only in conjunction with GIT_DIR
342 * without --bare. Catch the error early.
344 git_dir = getenv(GIT_DIR_ENVIRONMENT);
345 if ((!git_dir || is_bare_repository_cfg == 1)
346 && getenv(GIT_WORK_TREE_ENVIRONMENT))
347 die("%s (or --work-tree=<directory>) not allowed without "
348 "specifying %s (or --git-dir=<directory>)",
349 GIT_WORK_TREE_ENVIRONMENT,
350 GIT_DIR_ENVIRONMENT);
352 guess_repository_type(git_dir);
354 if (is_bare_repository_cfg <= 0) {
355 git_work_tree_cfg = xcalloc(PATH_MAX, 1);
356 if (!getcwd(git_work_tree_cfg, PATH_MAX))
357 die ("Cannot access current working directory.");
358 if (access(get_git_work_tree(), X_OK))
359 die ("Cannot access work tree '%s'",
360 get_git_work_tree());
364 * Set up the default .git directory contents
366 git_dir = getenv(GIT_DIR_ENVIRONMENT);
367 if (!git_dir)
368 git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
369 safe_create_dir(git_dir, 0);
371 /* Check to see if the repository version is right.
372 * Note that a newly created repository does not have
373 * config file, so this will not fail. What we are catching
374 * is an attempt to reinitialize new repository with an old tool.
376 check_repository_format();
378 reinit = create_default_files(git_dir, template_dir);
381 * And set up the object store.
383 sha1_dir = get_object_directory();
384 len = strlen(sha1_dir);
385 path = xmalloc(len + 40);
386 memcpy(path, sha1_dir, len);
388 safe_create_dir(sha1_dir, 1);
389 strcpy(path+len, "/pack");
390 safe_create_dir(path, 1);
391 strcpy(path+len, "/info");
392 safe_create_dir(path, 1);
394 if (shared_repository) {
395 char buf[10];
396 /* We do not spell "group" and such, so that
397 * the configuration can be read by older version
398 * of git.
400 sprintf(buf, "%d", shared_repository);
401 git_config_set("core.sharedrepository", buf);
402 git_config_set("receive.denyNonFastforwards", "true");
405 if (!quiet)
406 printf("%s%s Git repository in %s/\n",
407 reinit ? "Reinitialized existing" : "Initialized empty",
408 shared_repository ? " shared" : "",
409 git_dir);
411 return 0;