Allow a relative builtin template directory.
[git/mingw.git] / builtin-init-db.c
blobecf3db954a4f32ddfa836ffaf919b58e0835186f
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 close(fdo);
45 if (!status && adjust_shared_perm(dst))
46 return -1;
48 return status;
51 static void copy_templates_1(char *path, int baselen,
52 char *template, int template_baselen,
53 DIR *dir)
55 struct dirent *de;
57 /* Note: if ".git/hooks" file exists in the repository being
58 * re-initialized, /etc/core-git/templates/hooks/update would
59 * cause git-init to fail here. I think this is sane but
60 * it means that the set of templates we ship by default, along
61 * with the way the namespace under .git/ is organized, should
62 * be really carefully chosen.
64 safe_create_dir(path, 1);
65 while ((de = readdir(dir)) != NULL) {
66 struct stat st_git, st_template;
67 int namelen;
68 int exists = 0;
70 if (de->d_name[0] == '.')
71 continue;
72 namelen = strlen(de->d_name);
73 if ((PATH_MAX <= baselen + namelen) ||
74 (PATH_MAX <= template_baselen + namelen))
75 die("insanely long template name %s", de->d_name);
76 memcpy(path + baselen, de->d_name, namelen+1);
77 memcpy(template + template_baselen, de->d_name, namelen+1);
78 if (lstat(path, &st_git)) {
79 if (errno != ENOENT)
80 die("cannot stat %s", path);
82 else
83 exists = 1;
85 if (lstat(template, &st_template))
86 die("cannot stat template %s", template);
88 if (S_ISDIR(st_template.st_mode)) {
89 DIR *subdir = opendir(template);
90 int baselen_sub = baselen + namelen;
91 int template_baselen_sub = template_baselen + namelen;
92 if (!subdir)
93 die("cannot opendir %s", template);
94 path[baselen_sub++] =
95 template[template_baselen_sub++] = '/';
96 path[baselen_sub] =
97 template[template_baselen_sub] = 0;
98 copy_templates_1(path, baselen_sub,
99 template, template_baselen_sub,
100 subdir);
101 closedir(subdir);
103 else if (exists)
104 continue;
105 else if (S_ISLNK(st_template.st_mode)) {
106 char lnk[256];
107 int len;
108 len = readlink(template, lnk, sizeof(lnk));
109 if (len < 0)
110 die("cannot readlink %s", template);
111 if (sizeof(lnk) <= len)
112 die("insanely long symlink %s", template);
113 lnk[len] = 0;
114 if (symlink(lnk, path))
115 die("cannot symlink %s %s", lnk, path);
117 else if (S_ISREG(st_template.st_mode)) {
118 if (copy_file(path, template, st_template.st_mode))
119 die("cannot copy %s to %s", template, path);
121 else
122 error("ignoring template %s", template);
126 static void copy_templates(const char *git_dir, int len, const char *template_dir)
128 char path[PATH_MAX];
129 char template_path[PATH_MAX];
130 int template_len;
131 DIR *dir;
133 if (!template_dir)
134 template_dir = getenv(TEMPLATE_DIR_ENVIRONMENT);
135 if (!template_dir) {
137 * if the hard-coded template is relative, it is
138 * interpreted relative to the exec_dir
140 template_dir = DEFAULT_GIT_TEMPLATE_DIR;
141 if (template_dir[0] != '/' && template_dir[1] != ':') {
142 const char *exec_path = git_exec_path();
143 template_dir = prefix_path(exec_path, strlen(exec_path),
144 template_dir);
147 strcpy(template_path, template_dir);
148 template_len = strlen(template_path);
149 if (template_path[template_len-1] != '/') {
150 template_path[template_len++] = '/';
151 template_path[template_len] = 0;
153 dir = opendir(template_path);
154 if (!dir) {
155 fprintf(stderr, "warning: templates not found %s\n",
156 template_dir);
157 return;
160 /* Make sure that template is from the correct vintage */
161 strcpy(template_path + template_len, "config");
162 repository_format_version = 0;
163 git_config_from_file(check_repository_format_version,
164 template_path);
165 template_path[template_len] = 0;
167 if (repository_format_version &&
168 repository_format_version != GIT_REPO_VERSION) {
169 fprintf(stderr, "warning: not copying templates of "
170 "a wrong format version %d from '%s'\n",
171 repository_format_version,
172 template_dir);
173 closedir(dir);
174 return;
177 memcpy(path, git_dir, len);
178 path[len] = 0;
179 copy_templates_1(path, len,
180 template_path, template_len,
181 dir);
182 closedir(dir);
185 static int create_default_files(const char *git_dir, const char *template_path)
187 unsigned len = strlen(git_dir);
188 static char path[PATH_MAX];
189 unsigned char sha1[20];
190 struct stat st1;
191 char repo_version_string[10];
192 int reinit;
193 int filemode;
195 if (len > sizeof(path)-50)
196 die("insane git directory %s", git_dir);
197 memcpy(path, git_dir, len);
199 if (len && path[len-1] != '/')
200 path[len++] = '/';
203 * Create .git/refs/{heads,tags}
205 strcpy(path + len, "refs");
206 safe_create_dir(path, 1);
207 strcpy(path + len, "refs/heads");
208 safe_create_dir(path, 1);
209 strcpy(path + len, "refs/tags");
210 safe_create_dir(path, 1);
212 /* First copy the templates -- we might have the default
213 * config file there, in which case we would want to read
214 * from it after installing.
216 path[len] = 0;
217 copy_templates(path, len, template_path);
219 git_config(git_default_config);
222 * We would have created the above under user's umask -- under
223 * shared-repository settings, we would need to fix them up.
225 if (shared_repository) {
226 path[len] = 0;
227 adjust_shared_perm(path);
228 strcpy(path + len, "refs");
229 adjust_shared_perm(path);
230 strcpy(path + len, "refs/heads");
231 adjust_shared_perm(path);
232 strcpy(path + len, "refs/tags");
233 adjust_shared_perm(path);
237 * Create the default symlink from ".git/HEAD" to the "master"
238 * branch, if it does not exist yet.
240 strcpy(path + len, "HEAD");
241 reinit = !read_ref("HEAD", sha1);
242 if (!reinit) {
243 if (create_symref("HEAD", "refs/heads/master", NULL) < 0)
244 exit(1);
247 /* This forces creation of new config file */
248 sprintf(repo_version_string, "%d", GIT_REPO_VERSION);
249 git_config_set("core.repositoryformatversion", repo_version_string);
251 path[len] = 0;
252 strcpy(path + len, "config");
254 /* Check filemode trustability */
255 filemode = TEST_FILEMODE;
256 if (TEST_FILEMODE && !lstat(path, &st1)) {
257 struct stat st2;
258 filemode = (!chmod(path, st1.st_mode ^ S_IXUSR) &&
259 !lstat(path, &st2) &&
260 st1.st_mode != st2.st_mode);
262 git_config_set("core.filemode", filemode ? "true" : "false");
264 if (is_bare_repository()) {
265 git_config_set("core.bare", "true");
267 else {
268 git_config_set("core.bare", "false");
269 /* allow template config file to override the default */
270 if (log_all_ref_updates == -1)
271 git_config_set("core.logallrefupdates", "true");
273 return reinit;
276 static const char init_db_usage[] =
277 "git-init [--template=<template-directory>] [--shared]";
280 * If you want to, you can share the DB area with any number of branches.
281 * That has advantages: you can save space by sharing all the SHA1 objects.
282 * On the other hand, it might just make lookup slower and messier. You
283 * be the judge. The default case is to have one DB per managed directory.
285 int cmd_init_db(int argc, const char **argv, const char *prefix)
287 const char *git_dir;
288 const char *sha1_dir;
289 const char *template_dir = NULL;
290 char *path;
291 int len, i, reinit;
293 for (i = 1; i < argc; i++, argv++) {
294 const char *arg = argv[1];
295 if (!prefixcmp(arg, "--template="))
296 template_dir = arg+11;
297 else if (!strcmp(arg, "--shared"))
298 shared_repository = PERM_GROUP;
299 else if (!prefixcmp(arg, "--shared="))
300 shared_repository = git_config_perm("arg", arg+9);
301 else
302 usage(init_db_usage);
306 * Set up the default .git directory contents
308 git_dir = getenv(GIT_DIR_ENVIRONMENT);
309 if (!git_dir)
310 git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
311 safe_create_dir(git_dir, 0);
313 /* Check to see if the repository version is right.
314 * Note that a newly created repository does not have
315 * config file, so this will not fail. What we are catching
316 * is an attempt to reinitialize new repository with an old tool.
318 check_repository_format();
320 reinit = create_default_files(git_dir, template_dir);
323 * And set up the object store.
325 sha1_dir = get_object_directory();
326 len = strlen(sha1_dir);
327 path = xmalloc(len + 40);
328 memcpy(path, sha1_dir, len);
330 safe_create_dir(sha1_dir, 1);
331 strcpy(path+len, "/pack");
332 safe_create_dir(path, 1);
333 strcpy(path+len, "/info");
334 safe_create_dir(path, 1);
336 if (shared_repository) {
337 char buf[10];
338 /* We do not spell "group" and such, so that
339 * the configuration can be read by older version
340 * of git.
342 sprintf(buf, "%d", shared_repository);
343 git_config_set("core.sharedrepository", buf);
344 git_config_set("receive.denyNonFastforwards", "true");
347 printf("%s%s Git repository in %s/\n",
348 reinit ? "Reinitialized existing" : "Initialized empty",
349 shared_repository ? " shared" : "",
350 git_dir);
352 return 0;