]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-init-db.c
Release pack windows before reporting out of memory.
[thirdparty/git.git] / builtin-init-db.c
CommitLineData
8bc9a0c7
LT
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
e83c5163 6#include "cache.h"
c3c8835f 7#include "builtin.h"
e83c5163 8
d3af621b
JH
9#ifndef DEFAULT_GIT_TEMPLATE_DIR
10#define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates/"
11#endif
12
af6e277c 13static void safe_create_dir(const char *dir, int share)
cb126d8d 14{
f312de01 15 if (mkdir(dir, 0777) < 0) {
cb126d8d
ZW
16 if (errno != EEXIST) {
17 perror(dir);
18 exit(1);
19 }
20 }
af6e277c
JS
21 else if (share && adjust_shared_perm(dir))
22 die("Could not make %s writable by group\n", dir);
cb126d8d
ZW
23}
24
8d5afef0
JH
25static int copy_file(const char *dst, const char *src, int mode)
26{
32276c80 27 int fdi, fdo, status;
8d5afef0
JH
28
29 mode = (mode & 0111) ? 0777 : 0666;
30 if ((fdi = open(src, O_RDONLY)) < 0)
31 return fdi;
32 if ((fdo = open(dst, O_WRONLY | O_CREAT | O_EXCL, mode)) < 0) {
33 close(fdi);
34 return fdo;
35 }
32276c80 36 status = copy_fd(fdi, fdo);
8d5afef0 37 close(fdo);
af6e277c
JS
38
39 if (!status && adjust_shared_perm(dst))
40 return -1;
41
32276c80 42 return status;
8d5afef0
JH
43}
44
45static void copy_templates_1(char *path, int baselen,
46 char *template, int template_baselen,
47 DIR *dir)
48{
49 struct dirent *de;
50
51 /* Note: if ".git/hooks" file exists in the repository being
52 * re-initialized, /etc/core-git/templates/hooks/update would
53 * cause git-init-db to fail here. I think this is sane but
54 * it means that the set of templates we ship by default, along
55 * with the way the namespace under .git/ is organized, should
56 * be really carefully chosen.
57 */
af6e277c 58 safe_create_dir(path, 1);
8d5afef0
JH
59 while ((de = readdir(dir)) != NULL) {
60 struct stat st_git, st_template;
61 int namelen;
62 int exists = 0;
63
64 if (de->d_name[0] == '.')
65 continue;
66 namelen = strlen(de->d_name);
67 if ((PATH_MAX <= baselen + namelen) ||
68 (PATH_MAX <= template_baselen + namelen))
69 die("insanely long template name %s", de->d_name);
70 memcpy(path + baselen, de->d_name, namelen+1);
71 memcpy(template + template_baselen, de->d_name, namelen+1);
72 if (lstat(path, &st_git)) {
73 if (errno != ENOENT)
74 die("cannot stat %s", path);
75 }
76 else
77 exists = 1;
78
79 if (lstat(template, &st_template))
80 die("cannot stat template %s", template);
81
82 if (S_ISDIR(st_template.st_mode)) {
83 DIR *subdir = opendir(template);
84 int baselen_sub = baselen + namelen;
85 int template_baselen_sub = template_baselen + namelen;
86 if (!subdir)
87 die("cannot opendir %s", template);
88 path[baselen_sub++] =
89 template[template_baselen_sub++] = '/';
90 path[baselen_sub] =
91 template[template_baselen_sub] = 0;
92 copy_templates_1(path, baselen_sub,
93 template, template_baselen_sub,
94 subdir);
95 closedir(subdir);
96 }
97 else if (exists)
98 continue;
99 else if (S_ISLNK(st_template.st_mode)) {
100 char lnk[256];
101 int len;
102 len = readlink(template, lnk, sizeof(lnk));
103 if (len < 0)
104 die("cannot readlink %s", template);
105 if (sizeof(lnk) <= len)
106 die("insanely long symlink %s", template);
107 lnk[len] = 0;
108 if (symlink(lnk, path))
109 die("cannot symlink %s %s", lnk, path);
110 }
111 else if (S_ISREG(st_template.st_mode)) {
112 if (copy_file(path, template, st_template.st_mode))
113 die("cannot copy %s to %s", template, path);
114 }
115 else
116 error("ignoring template %s", template);
117 }
118}
119
c3c8835f 120static void copy_templates(const char *git_dir, int len, const char *template_dir)
8d5afef0
JH
121{
122 char path[PATH_MAX];
123 char template_path[PATH_MAX];
d3af621b 124 int template_len;
8d5afef0
JH
125 DIR *dir;
126
8683a45d 127 if (!template_dir) {
d4ebc36c 128 template_dir = getenv(TEMPLATE_DIR_ENVIRONMENT);
8683a45d
JS
129 if (!template_dir)
130 template_dir = DEFAULT_GIT_TEMPLATE_DIR;
131 }
8d5afef0
JH
132 strcpy(template_path, template_dir);
133 template_len = strlen(template_path);
134 if (template_path[template_len-1] != '/') {
135 template_path[template_len++] = '/';
136 template_path[template_len] = 0;
137 }
8d5afef0 138 dir = opendir(template_path);
d3af621b
JH
139 if (!dir) {
140 fprintf(stderr, "warning: templates not found %s\n",
141 template_dir);
8d5afef0 142 return;
d3af621b
JH
143 }
144
4f629539
JH
145 /* Make sure that template is from the correct vintage */
146 strcpy(template_path + template_len, "config");
147 repository_format_version = 0;
148 git_config_from_file(check_repository_format_version,
149 template_path);
150 template_path[template_len] = 0;
151
152 if (repository_format_version &&
153 repository_format_version != GIT_REPO_VERSION) {
154 fprintf(stderr, "warning: not copying templates of "
155 "a wrong format version %d from '%s'\n",
156 repository_format_version,
157 template_dir);
158 closedir(dir);
159 return;
160 }
161
d3af621b 162 memcpy(path, git_dir, len);
1f961c19 163 path[len] = 0;
8d5afef0
JH
164 copy_templates_1(path, len,
165 template_path, template_len,
166 dir);
167 closedir(dir);
168}
169
ef0a89a6 170static int create_default_files(const char *git_dir, const char *template_path)
cad88fdf
LT
171{
172 unsigned len = strlen(git_dir);
173 static char path[PATH_MAX];
8098a178 174 unsigned char sha1[20];
4f629539
JH
175 struct stat st1;
176 char repo_version_string[10];
ef0a89a6 177 int reinit;
cad88fdf
LT
178
179 if (len > sizeof(path)-50)
180 die("insane git directory %s", git_dir);
181 memcpy(path, git_dir, len);
182
183 if (len && path[len-1] != '/')
184 path[len++] = '/';
185
186 /*
187 * Create .git/refs/{heads,tags}
188 */
189 strcpy(path + len, "refs");
af6e277c 190 safe_create_dir(path, 1);
cad88fdf 191 strcpy(path + len, "refs/heads");
af6e277c 192 safe_create_dir(path, 1);
cad88fdf 193 strcpy(path + len, "refs/tags");
af6e277c 194 safe_create_dir(path, 1);
cad88fdf 195
4f629539
JH
196 /* First copy the templates -- we might have the default
197 * config file there, in which case we would want to read
198 * from it after installing.
199 */
200 path[len] = 0;
201 copy_templates(path, len, template_path);
202
203 git_config(git_default_config);
204
138086a7
JH
205 /*
206 * We would have created the above under user's umask -- under
207 * shared-repository settings, we would need to fix them up.
208 */
209 if (shared_repository) {
210 path[len] = 0;
211 adjust_shared_perm(path);
212 strcpy(path + len, "refs");
213 adjust_shared_perm(path);
214 strcpy(path + len, "refs/heads");
215 adjust_shared_perm(path);
216 strcpy(path + len, "refs/tags");
217 adjust_shared_perm(path);
218 }
219
cad88fdf
LT
220 /*
221 * Create the default symlink from ".git/HEAD" to the "master"
8098a178 222 * branch, if it does not exist yet.
cad88fdf
LT
223 */
224 strcpy(path + len, "HEAD");
ef0a89a6
SP
225 reinit = !read_ref("HEAD", sha1);
226 if (!reinit) {
ed378ec7 227 if (create_symref("HEAD", "refs/heads/master") < 0)
cad88fdf 228 exit(1);
cad88fdf 229 }
e24317b4 230
4f629539
JH
231 /* This forces creation of new config file */
232 sprintf(repo_version_string, "%d", GIT_REPO_VERSION);
233 git_config_set("core.repositoryformatversion", repo_version_string);
234
235 path[len] = 0;
e24317b4 236 strcpy(path + len, "config");
4f629539
JH
237
238 /* Check filemode trustability */
239 if (!lstat(path, &st1)) {
240 struct stat st2;
241 int filemode = (!chmod(path, st1.st_mode ^ S_IXUSR) &&
242 !lstat(path, &st2) &&
243 st1.st_mode != st2.st_mode);
244 git_config_set("core.filemode",
245 filemode ? "true" : "false");
e24317b4 246 }
ef0a89a6 247
0bee5918
SP
248 /* Enable logAllRefUpdates if a working tree is attached */
249 if (!is_bare_git_dir(git_dir))
250 git_config_set("core.logallrefupdates", "true");
ef0a89a6 251 return reinit;
cad88fdf
LT
252}
253
d3af621b 254static const char init_db_usage[] =
af6e277c 255"git-init-db [--template=<template-directory>] [--shared]";
d3af621b 256
4696cb93
ZW
257/*
258 * If you want to, you can share the DB area with any number of branches.
259 * That has advantages: you can save space by sharing all the SHA1 objects.
260 * On the other hand, it might just make lookup slower and messier. You
261 * be the judge. The default case is to have one DB per managed directory.
262 */
a633fca0 263int cmd_init_db(int argc, const char **argv, const char *prefix)
e83c5163 264{
cad88fdf 265 const char *git_dir;
d19938ab 266 const char *sha1_dir;
c3c8835f
TH
267 const char *template_dir = NULL;
268 char *path;
ef0a89a6 269 int len, i, reinit;
e83c5163 270
d3af621b 271 for (i = 1; i < argc; i++, argv++) {
c3c8835f 272 const char *arg = argv[1];
4a62eaed 273 if (!strncmp(arg, "--template=", 11))
d3af621b 274 template_dir = arg+11;
af6e277c 275 else if (!strcmp(arg, "--shared"))
94df2506
JH
276 shared_repository = PERM_GROUP;
277 else if (!strncmp(arg, "--shared=", 9))
278 shared_repository = git_config_perm("arg", arg+9);
d3af621b 279 else
8cdf3364 280 usage(init_db_usage);
d3af621b
JH
281 }
282
cad88fdf
LT
283 /*
284 * Set up the default .git directory contents
285 */
a9ab586a 286 git_dir = getenv(GIT_DIR_ENVIRONMENT);
ef0a89a6 287 if (!git_dir)
cad88fdf 288 git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
af6e277c 289 safe_create_dir(git_dir, 0);
4f629539
JH
290
291 /* Check to see if the repository version is right.
292 * Note that a newly created repository does not have
293 * config file, so this will not fail. What we are catching
294 * is an attempt to reinitialize new repository with an old tool.
295 */
296 check_repository_format();
297
ef0a89a6 298 reinit = create_default_files(git_dir, template_dir);
cad88fdf
LT
299
300 /*
301 * And set up the object store.
302 */
303 sha1_dir = get_object_directory();
e83c5163 304 len = strlen(sha1_dir);
812666c8 305 path = xmalloc(len + 40);
e83c5163 306 memcpy(path, sha1_dir, len);
cb126d8d 307
af6e277c 308 safe_create_dir(sha1_dir, 1);
f49fb35d 309 strcpy(path+len, "/pack");
af6e277c 310 safe_create_dir(path, 1);
d57306c7 311 strcpy(path+len, "/info");
af6e277c
JS
312 safe_create_dir(path, 1);
313
94df2506
JH
314 if (shared_repository) {
315 char buf[10];
316 /* We do not spell "group" and such, so that
317 * the configuration can be read by older version
318 * of git.
319 */
320 sprintf(buf, "%d", shared_repository);
321 git_config_set("core.sharedrepository", buf);
11031d7e 322 git_config_set("receive.denyNonFastforwards", "true");
94df2506 323 }
af6e277c 324
ef0a89a6
SP
325 printf("%s%s Git repository in %s/\n",
326 reinit ? "Reinitialized existing" : "Initialized empty",
327 shared_repository ? " shared" : "",
328 git_dir);
329
e83c5163
LT
330 return 0;
331}