]> git.ipfire.org Git - thirdparty/git.git/blame - init-db.c
Check repository format version in enter_repo().
[thirdparty/git.git] / init-db.c
CommitLineData
8bc9a0c7
LT
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
e83c5163
LT
6#include "cache.h"
7
d3af621b
JH
8#ifndef DEFAULT_GIT_TEMPLATE_DIR
9#define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates/"
10#endif
11
e99d59ff 12static void safe_create_dir(const char *dir)
cb126d8d 13{
f312de01 14 if (mkdir(dir, 0777) < 0) {
cb126d8d
ZW
15 if (errno != EEXIST) {
16 perror(dir);
17 exit(1);
18 }
19 }
20}
21
8d5afef0
JH
22static int copy_file(const char *dst, const char *src, int mode)
23{
32276c80 24 int fdi, fdo, status;
8d5afef0
JH
25
26 mode = (mode & 0111) ? 0777 : 0666;
27 if ((fdi = open(src, O_RDONLY)) < 0)
28 return fdi;
29 if ((fdo = open(dst, O_WRONLY | O_CREAT | O_EXCL, mode)) < 0) {
30 close(fdi);
31 return fdo;
32 }
32276c80 33 status = copy_fd(fdi, fdo);
8d5afef0 34 close(fdo);
32276c80 35 return status;
8d5afef0
JH
36}
37
38static void copy_templates_1(char *path, int baselen,
39 char *template, int template_baselen,
40 DIR *dir)
41{
42 struct dirent *de;
43
44 /* Note: if ".git/hooks" file exists in the repository being
45 * re-initialized, /etc/core-git/templates/hooks/update would
46 * cause git-init-db to fail here. I think this is sane but
47 * it means that the set of templates we ship by default, along
48 * with the way the namespace under .git/ is organized, should
49 * be really carefully chosen.
50 */
51 safe_create_dir(path);
52 while ((de = readdir(dir)) != NULL) {
53 struct stat st_git, st_template;
54 int namelen;
55 int exists = 0;
56
57 if (de->d_name[0] == '.')
58 continue;
59 namelen = strlen(de->d_name);
60 if ((PATH_MAX <= baselen + namelen) ||
61 (PATH_MAX <= template_baselen + namelen))
62 die("insanely long template name %s", de->d_name);
63 memcpy(path + baselen, de->d_name, namelen+1);
64 memcpy(template + template_baselen, de->d_name, namelen+1);
65 if (lstat(path, &st_git)) {
66 if (errno != ENOENT)
67 die("cannot stat %s", path);
68 }
69 else
70 exists = 1;
71
72 if (lstat(template, &st_template))
73 die("cannot stat template %s", template);
74
75 if (S_ISDIR(st_template.st_mode)) {
76 DIR *subdir = opendir(template);
77 int baselen_sub = baselen + namelen;
78 int template_baselen_sub = template_baselen + namelen;
79 if (!subdir)
80 die("cannot opendir %s", template);
81 path[baselen_sub++] =
82 template[template_baselen_sub++] = '/';
83 path[baselen_sub] =
84 template[template_baselen_sub] = 0;
85 copy_templates_1(path, baselen_sub,
86 template, template_baselen_sub,
87 subdir);
88 closedir(subdir);
89 }
90 else if (exists)
91 continue;
92 else if (S_ISLNK(st_template.st_mode)) {
93 char lnk[256];
94 int len;
95 len = readlink(template, lnk, sizeof(lnk));
96 if (len < 0)
97 die("cannot readlink %s", template);
98 if (sizeof(lnk) <= len)
99 die("insanely long symlink %s", template);
100 lnk[len] = 0;
101 if (symlink(lnk, path))
102 die("cannot symlink %s %s", lnk, path);
103 }
104 else if (S_ISREG(st_template.st_mode)) {
105 if (copy_file(path, template, st_template.st_mode))
106 die("cannot copy %s to %s", template, path);
107 }
108 else
109 error("ignoring template %s", template);
110 }
111}
112
d3af621b 113static void copy_templates(const char *git_dir, int len, char *template_dir)
8d5afef0
JH
114{
115 char path[PATH_MAX];
116 char template_path[PATH_MAX];
d3af621b 117 int template_len;
8d5afef0
JH
118 DIR *dir;
119
8d5afef0 120 if (!template_dir)
d3af621b 121 template_dir = DEFAULT_GIT_TEMPLATE_DIR;
8d5afef0
JH
122 strcpy(template_path, template_dir);
123 template_len = strlen(template_path);
124 if (template_path[template_len-1] != '/') {
125 template_path[template_len++] = '/';
126 template_path[template_len] = 0;
127 }
8d5afef0 128 dir = opendir(template_path);
d3af621b
JH
129 if (!dir) {
130 fprintf(stderr, "warning: templates not found %s\n",
131 template_dir);
8d5afef0 132 return;
d3af621b
JH
133 }
134
135 memcpy(path, git_dir, len);
1f961c19 136 path[len] = 0;
8d5afef0
JH
137 copy_templates_1(path, len,
138 template_path, template_len,
139 dir);
140 closedir(dir);
141}
142
d3af621b
JH
143static void create_default_files(const char *git_dir,
144 char *template_path)
cad88fdf
LT
145{
146 unsigned len = strlen(git_dir);
147 static char path[PATH_MAX];
8098a178 148 unsigned char sha1[20];
cad88fdf
LT
149
150 if (len > sizeof(path)-50)
151 die("insane git directory %s", git_dir);
152 memcpy(path, git_dir, len);
153
154 if (len && path[len-1] != '/')
155 path[len++] = '/';
156
157 /*
158 * Create .git/refs/{heads,tags}
159 */
160 strcpy(path + len, "refs");
161 safe_create_dir(path);
162 strcpy(path + len, "refs/heads");
163 safe_create_dir(path);
164 strcpy(path + len, "refs/tags");
165 safe_create_dir(path);
166
167 /*
168 * Create the default symlink from ".git/HEAD" to the "master"
8098a178 169 * branch, if it does not exist yet.
cad88fdf
LT
170 */
171 strcpy(path + len, "HEAD");
8098a178
JH
172 if (read_ref(path, sha1) < 0) {
173 if (create_symref(path, "refs/heads/master") < 0)
cad88fdf 174 exit(1);
cad88fdf 175 }
8098a178 176 path[len] = 0;
d3af621b 177 copy_templates(path, len, template_path);
e24317b4
JS
178
179 /*
180 * Find out if we can trust the executable bit.
181 */
182 safe_create_dir(path);
183 strcpy(path + len, "config");
184 if (access(path, R_OK) < 0) {
185 static const char contents[] =
186 "#\n"
187 "# This is the config file\n"
188 "#\n"
189 "\n"
190 "; core variables\n"
191 "[core]\n"
192 " ; Don't trust file modes\n"
193 " filemode = false\n"
194 "\n";
195 FILE *config = fopen(path, "w");
196 struct stat st;
197
198 if (!config)
199 die("Can not write to %s?", path);
200
201 fwrite(contents, sizeof(contents)-1, 1, config);
202
203 fclose(config);
204
205 if (!lstat(path, &st)) {
206 struct stat st2;
207 if (!chmod(path, st.st_mode ^ S_IXUSR) &&
208 !lstat(path, &st2) &&
209 st.st_mode != st2.st_mode)
210 unlink(path);
211 else
212 fprintf(stderr, "Ignoring file modes\n");
213 }
214 }
cad88fdf
LT
215}
216
d3af621b
JH
217static const char init_db_usage[] =
218"git-init-db [--template=<template-directory>]";
219
4696cb93
ZW
220/*
221 * If you want to, you can share the DB area with any number of branches.
222 * That has advantages: you can save space by sharing all the SHA1 objects.
223 * On the other hand, it might just make lookup slower and messier. You
224 * be the judge. The default case is to have one DB per managed directory.
225 */
e83c5163
LT
226int main(int argc, char **argv)
227{
cad88fdf 228 const char *git_dir;
d19938ab 229 const char *sha1_dir;
d3af621b 230 char *path, *template_dir = NULL;
19b2860c 231 int len, i;
e83c5163 232
d3af621b
JH
233 for (i = 1; i < argc; i++, argv++) {
234 char *arg = argv[1];
235 if (arg[0] != '-')
236 break;
237 else if (!strncmp(arg, "--template=", 11))
238 template_dir = arg+11;
239 else
240 die(init_db_usage);
241 }
242
cad88fdf
LT
243 /*
244 * Set up the default .git directory contents
245 */
a9ab586a 246 git_dir = getenv(GIT_DIR_ENVIRONMENT);
cad88fdf
LT
247 if (!git_dir) {
248 git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
addb315d 249 fprintf(stderr, "defaulting to local storage area\n");
e83c5163 250 }
cad88fdf 251 safe_create_dir(git_dir);
d3af621b 252 create_default_files(git_dir, template_dir);
cad88fdf
LT
253
254 /*
255 * And set up the object store.
256 */
257 sha1_dir = get_object_directory();
e83c5163 258 len = strlen(sha1_dir);
812666c8 259 path = xmalloc(len + 40);
e83c5163 260 memcpy(path, sha1_dir, len);
cb126d8d
ZW
261
262 safe_create_dir(sha1_dir);
f49fb35d
LT
263 strcpy(path+len, "/pack");
264 safe_create_dir(path);
d57306c7
JH
265 strcpy(path+len, "/info");
266 safe_create_dir(path);
e83c5163
LT
267 return 0;
268}