]>
Commit | Line | Data |
---|---|---|
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" |
a47d1813 | 8 | #include "exec_cmd.h" |
e83c5163 | 9 | |
d3af621b | 10 | #ifndef DEFAULT_GIT_TEMPLATE_DIR |
d52fd42a | 11 | #define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates" |
d3af621b JH |
12 | #endif |
13 | ||
c869753e SP |
14 | #ifdef NO_TRUSTABLE_FILEMODE |
15 | #define TEST_FILEMODE 0 | |
16 | #else | |
17 | #define TEST_FILEMODE 1 | |
18 | #endif | |
19 | ||
0a2c7eea DM |
20 | static int init_is_bare_repository = 0; |
21 | static int init_shared_repository = -1; | |
22 | ||
af6e277c | 23 | static void safe_create_dir(const char *dir, int share) |
cb126d8d | 24 | { |
f312de01 | 25 | if (mkdir(dir, 0777) < 0) { |
cb126d8d ZW |
26 | if (errno != EEXIST) { |
27 | perror(dir); | |
28 | exit(1); | |
29 | } | |
30 | } | |
af6e277c | 31 | else if (share && adjust_shared_perm(dir)) |
d7530708 | 32 | die("Could not make %s writable by group", dir); |
cb126d8d ZW |
33 | } |
34 | ||
8d5afef0 JH |
35 | static void copy_templates_1(char *path, int baselen, |
36 | char *template, int template_baselen, | |
37 | DIR *dir) | |
38 | { | |
39 | struct dirent *de; | |
40 | ||
41 | /* Note: if ".git/hooks" file exists in the repository being | |
42 | * re-initialized, /etc/core-git/templates/hooks/update would | |
f18d244a | 43 | * cause "git init" to fail here. I think this is sane but |
8d5afef0 JH |
44 | * it means that the set of templates we ship by default, along |
45 | * with the way the namespace under .git/ is organized, should | |
46 | * be really carefully chosen. | |
47 | */ | |
af6e277c | 48 | safe_create_dir(path, 1); |
8d5afef0 JH |
49 | while ((de = readdir(dir)) != NULL) { |
50 | struct stat st_git, st_template; | |
51 | int namelen; | |
52 | int exists = 0; | |
53 | ||
54 | if (de->d_name[0] == '.') | |
55 | continue; | |
56 | namelen = strlen(de->d_name); | |
57 | if ((PATH_MAX <= baselen + namelen) || | |
58 | (PATH_MAX <= template_baselen + namelen)) | |
59 | die("insanely long template name %s", de->d_name); | |
60 | memcpy(path + baselen, de->d_name, namelen+1); | |
61 | memcpy(template + template_baselen, de->d_name, namelen+1); | |
62 | if (lstat(path, &st_git)) { | |
63 | if (errno != ENOENT) | |
0721c314 | 64 | die_errno("cannot stat '%s'", path); |
8d5afef0 JH |
65 | } |
66 | else | |
67 | exists = 1; | |
68 | ||
69 | if (lstat(template, &st_template)) | |
0721c314 | 70 | die_errno("cannot stat template '%s'", template); |
8d5afef0 JH |
71 | |
72 | if (S_ISDIR(st_template.st_mode)) { | |
73 | DIR *subdir = opendir(template); | |
74 | int baselen_sub = baselen + namelen; | |
75 | int template_baselen_sub = template_baselen + namelen; | |
76 | if (!subdir) | |
0721c314 | 77 | die_errno("cannot opendir '%s'", template); |
8d5afef0 JH |
78 | path[baselen_sub++] = |
79 | template[template_baselen_sub++] = '/'; | |
80 | path[baselen_sub] = | |
81 | template[template_baselen_sub] = 0; | |
82 | copy_templates_1(path, baselen_sub, | |
83 | template, template_baselen_sub, | |
84 | subdir); | |
85 | closedir(subdir); | |
86 | } | |
87 | else if (exists) | |
88 | continue; | |
89 | else if (S_ISLNK(st_template.st_mode)) { | |
90 | char lnk[256]; | |
91 | int len; | |
92 | len = readlink(template, lnk, sizeof(lnk)); | |
93 | if (len < 0) | |
0721c314 | 94 | die_errno("cannot readlink '%s'", template); |
8d5afef0 JH |
95 | if (sizeof(lnk) <= len) |
96 | die("insanely long symlink %s", template); | |
97 | lnk[len] = 0; | |
98 | if (symlink(lnk, path)) | |
0721c314 | 99 | die_errno("cannot symlink '%s' '%s'", lnk, path); |
8d5afef0 JH |
100 | } |
101 | else if (S_ISREG(st_template.st_mode)) { | |
102 | if (copy_file(path, template, st_template.st_mode)) | |
0721c314 TR |
103 | die_errno("cannot copy '%s' to '%s'", template, |
104 | path); | |
8d5afef0 JH |
105 | } |
106 | else | |
107 | error("ignoring template %s", template); | |
108 | } | |
109 | } | |
110 | ||
f225aeb2 | 111 | static void copy_templates(const char *template_dir) |
8d5afef0 JH |
112 | { |
113 | char path[PATH_MAX]; | |
114 | char template_path[PATH_MAX]; | |
d3af621b | 115 | int template_len; |
8d5afef0 | 116 | DIR *dir; |
f225aeb2 DB |
117 | const char *git_dir = get_git_dir(); |
118 | int len = strlen(git_dir); | |
8d5afef0 | 119 | |
a47d1813 | 120 | if (!template_dir) |
d4ebc36c | 121 | template_dir = getenv(TEMPLATE_DIR_ENVIRONMENT); |
2de9de5e SP |
122 | if (!template_dir) |
123 | template_dir = system_path(DEFAULT_GIT_TEMPLATE_DIR); | |
172035f0 JK |
124 | if (!template_dir[0]) |
125 | return; | |
32d1776b FL |
126 | template_len = strlen(template_dir); |
127 | if (PATH_MAX <= (template_len+strlen("/config"))) | |
128 | die("insanely long template path %s", template_dir); | |
8d5afef0 | 129 | strcpy(template_path, template_dir); |
8d5afef0 JH |
130 | if (template_path[template_len-1] != '/') { |
131 | template_path[template_len++] = '/'; | |
132 | template_path[template_len] = 0; | |
133 | } | |
8d5afef0 | 134 | dir = opendir(template_path); |
d3af621b | 135 | if (!dir) { |
2fd8c0a5 | 136 | warning("templates not found %s", template_dir); |
8d5afef0 | 137 | return; |
d3af621b JH |
138 | } |
139 | ||
4f629539 JH |
140 | /* Make sure that template is from the correct vintage */ |
141 | strcpy(template_path + template_len, "config"); | |
142 | repository_format_version = 0; | |
143 | git_config_from_file(check_repository_format_version, | |
ef90d6d4 | 144 | template_path, NULL); |
4f629539 JH |
145 | template_path[template_len] = 0; |
146 | ||
147 | if (repository_format_version && | |
148 | repository_format_version != GIT_REPO_VERSION) { | |
2fd8c0a5 MV |
149 | warning("not copying templates of " |
150 | "a wrong format version %d from '%s'", | |
4f629539 JH |
151 | repository_format_version, |
152 | template_dir); | |
153 | closedir(dir); | |
154 | return; | |
155 | } | |
156 | ||
d3af621b | 157 | memcpy(path, git_dir, len); |
f225aeb2 DB |
158 | if (len && path[len - 1] != '/') |
159 | path[len++] = '/'; | |
1f961c19 | 160 | path[len] = 0; |
8d5afef0 JH |
161 | copy_templates_1(path, len, |
162 | template_path, template_len, | |
163 | dir); | |
164 | closedir(dir); | |
165 | } | |
166 | ||
f225aeb2 | 167 | static int create_default_files(const char *template_path) |
cad88fdf | 168 | { |
f225aeb2 | 169 | const char *git_dir = get_git_dir(); |
cad88fdf LT |
170 | unsigned len = strlen(git_dir); |
171 | static char path[PATH_MAX]; | |
4f629539 JH |
172 | struct stat st1; |
173 | char repo_version_string[10]; | |
5cc8f372 | 174 | char junk[2]; |
ef0a89a6 | 175 | int reinit; |
c869753e | 176 | int filemode; |
cad88fdf LT |
177 | |
178 | if (len > sizeof(path)-50) | |
179 | die("insane git directory %s", git_dir); | |
180 | memcpy(path, git_dir, len); | |
181 | ||
182 | if (len && path[len-1] != '/') | |
183 | path[len++] = '/'; | |
184 | ||
185 | /* | |
186 | * Create .git/refs/{heads,tags} | |
187 | */ | |
f225aeb2 DB |
188 | safe_create_dir(git_path("refs"), 1); |
189 | safe_create_dir(git_path("refs/heads"), 1); | |
190 | safe_create_dir(git_path("refs/tags"), 1); | |
cad88fdf | 191 | |
4f629539 JH |
192 | /* First copy the templates -- we might have the default |
193 | * config file there, in which case we would want to read | |
194 | * from it after installing. | |
195 | */ | |
f225aeb2 | 196 | copy_templates(template_path); |
4f629539 | 197 | |
ef90d6d4 | 198 | git_config(git_default_config, NULL); |
0a2c7eea | 199 | is_bare_repository_cfg = init_is_bare_repository; |
5a688fe4 JH |
200 | |
201 | /* reading existing config may have overwrote it */ | |
0a2c7eea DM |
202 | if (init_shared_repository != -1) |
203 | shared_repository = init_shared_repository; | |
4f629539 | 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) { | |
f225aeb2 DB |
210 | adjust_shared_perm(get_git_dir()); |
211 | adjust_shared_perm(git_path("refs")); | |
212 | adjust_shared_perm(git_path("refs/heads")); | |
213 | adjust_shared_perm(git_path("refs/tags")); | |
138086a7 JH |
214 | } |
215 | ||
cad88fdf LT |
216 | /* |
217 | * Create the default symlink from ".git/HEAD" to the "master" | |
8098a178 | 218 | * branch, if it does not exist yet. |
cad88fdf LT |
219 | */ |
220 | strcpy(path + len, "HEAD"); | |
5cc8f372 JS |
221 | reinit = (!access(path, R_OK) |
222 | || readlink(path, junk, sizeof(junk)-1) != -1); | |
ef0a89a6 | 223 | if (!reinit) { |
8b5157e4 | 224 | if (create_symref("HEAD", "refs/heads/master", NULL) < 0) |
cad88fdf | 225 | exit(1); |
cad88fdf | 226 | } |
e24317b4 | 227 | |
4f629539 JH |
228 | /* This forces creation of new config file */ |
229 | sprintf(repo_version_string, "%d", GIT_REPO_VERSION); | |
230 | git_config_set("core.repositoryformatversion", repo_version_string); | |
231 | ||
232 | path[len] = 0; | |
e24317b4 | 233 | strcpy(path + len, "config"); |
4f629539 JH |
234 | |
235 | /* Check filemode trustability */ | |
c869753e SP |
236 | filemode = TEST_FILEMODE; |
237 | if (TEST_FILEMODE && !lstat(path, &st1)) { | |
4f629539 | 238 | struct stat st2; |
c869753e | 239 | filemode = (!chmod(path, st1.st_mode ^ S_IXUSR) && |
4f629539 JH |
240 | !lstat(path, &st2) && |
241 | st1.st_mode != st2.st_mode); | |
e24317b4 | 242 | } |
c869753e | 243 | git_config_set("core.filemode", filemode ? "true" : "false"); |
ef0a89a6 | 244 | |
e90fdc39 | 245 | if (is_bare_repository()) |
7d1864ce | 246 | git_config_set("core.bare", "true"); |
7d1864ce | 247 | else { |
e90fdc39 | 248 | const char *work_tree = get_git_work_tree(); |
7d1864ce | 249 | git_config_set("core.bare", "false"); |
196055c2 AR |
250 | /* allow template config file to override the default */ |
251 | if (log_all_ref_updates == -1) | |
252 | git_config_set("core.logallrefupdates", "true"); | |
f225aeb2 DB |
253 | if (prefixcmp(git_dir, work_tree) || |
254 | strcmp(git_dir + strlen(work_tree), "/.git")) { | |
e90fdc39 | 255 | git_config_set("core.worktree", work_tree); |
f225aeb2 | 256 | } |
7d1864ce | 257 | } |
75d24499 | 258 | |
75d24499 | 259 | if (!reinit) { |
2455406a | 260 | /* Check if symlink is supported in the work tree */ |
75d24499 JH |
261 | path[len] = 0; |
262 | strcpy(path + len, "tXXXXXX"); | |
263 | if (!close(xmkstemp(path)) && | |
264 | !unlink(path) && | |
265 | !symlink("testing", path) && | |
266 | !lstat(path, &st1) && | |
267 | S_ISLNK(st1.st_mode)) | |
268 | unlink(path); /* good */ | |
269 | else | |
270 | git_config_set("core.symlinks", "false"); | |
2455406a DP |
271 | |
272 | /* Check if the filesystem is case-insensitive */ | |
273 | path[len] = 0; | |
274 | strcpy(path + len, "CoNfIg"); | |
275 | if (!access(path, F_OK)) | |
276 | git_config_set("core.ignorecase", "true"); | |
75d24499 JH |
277 | } |
278 | ||
ef0a89a6 | 279 | return reinit; |
cad88fdf LT |
280 | } |
281 | ||
f225aeb2 DB |
282 | int init_db(const char *template_dir, unsigned int flags) |
283 | { | |
284 | const char *sha1_dir; | |
285 | char *path; | |
286 | int len, reinit; | |
287 | ||
288 | safe_create_dir(get_git_dir(), 0); | |
289 | ||
0a2c7eea DM |
290 | init_is_bare_repository = is_bare_repository(); |
291 | ||
f225aeb2 DB |
292 | /* Check to see if the repository version is right. |
293 | * Note that a newly created repository does not have | |
294 | * config file, so this will not fail. What we are catching | |
295 | * is an attempt to reinitialize new repository with an old tool. | |
296 | */ | |
297 | check_repository_format(); | |
298 | ||
299 | reinit = create_default_files(template_dir); | |
300 | ||
301 | sha1_dir = get_object_directory(); | |
302 | len = strlen(sha1_dir); | |
303 | path = xmalloc(len + 40); | |
304 | memcpy(path, sha1_dir, len); | |
305 | ||
306 | safe_create_dir(sha1_dir, 1); | |
307 | strcpy(path+len, "/pack"); | |
308 | safe_create_dir(path, 1); | |
309 | strcpy(path+len, "/info"); | |
310 | safe_create_dir(path, 1); | |
311 | ||
312 | if (shared_repository) { | |
313 | char buf[10]; | |
314 | /* We do not spell "group" and such, so that | |
315 | * the configuration can be read by older version | |
316 | * of git. Note, we use octal numbers for new share modes, | |
317 | * and compatibility values for PERM_GROUP and | |
318 | * PERM_EVERYBODY. | |
319 | */ | |
5a688fe4 JH |
320 | if (shared_repository < 0) |
321 | /* force to the mode value */ | |
322 | sprintf(buf, "0%o", -shared_repository); | |
323 | else if (shared_repository == PERM_GROUP) | |
f225aeb2 DB |
324 | sprintf(buf, "%d", OLD_PERM_GROUP); |
325 | else if (shared_repository == PERM_EVERYBODY) | |
326 | sprintf(buf, "%d", OLD_PERM_EVERYBODY); | |
327 | else | |
5a688fe4 | 328 | die("oops"); |
f225aeb2 DB |
329 | git_config_set("core.sharedrepository", buf); |
330 | git_config_set("receive.denyNonFastforwards", "true"); | |
331 | } | |
332 | ||
333 | if (!(flags & INIT_DB_QUIET)) | |
334 | printf("%s%s Git repository in %s/\n", | |
335 | reinit ? "Reinitialized existing" : "Initialized empty", | |
336 | shared_repository ? " shared" : "", | |
337 | get_git_dir()); | |
338 | ||
339 | return 0; | |
340 | } | |
341 | ||
342 | static int guess_repository_type(const char *git_dir) | |
6adcca3f JH |
343 | { |
344 | char cwd[PATH_MAX]; | |
345 | const char *slash; | |
346 | ||
6adcca3f JH |
347 | /* |
348 | * "GIT_DIR=. git init" is always bare. | |
349 | * "GIT_DIR=`pwd` git init" too. | |
350 | */ | |
351 | if (!strcmp(".", git_dir)) | |
f225aeb2 | 352 | return 1; |
6adcca3f | 353 | if (!getcwd(cwd, sizeof(cwd))) |
0721c314 | 354 | die_errno("cannot tell cwd"); |
6adcca3f | 355 | if (!strcmp(git_dir, cwd)) |
f225aeb2 | 356 | return 1; |
6adcca3f JH |
357 | /* |
358 | * "GIT_DIR=.git or GIT_DIR=something/.git is usually not. | |
359 | */ | |
360 | if (!strcmp(git_dir, ".git")) | |
f225aeb2 | 361 | return 0; |
6adcca3f JH |
362 | slash = strrchr(git_dir, '/'); |
363 | if (slash && !strcmp(slash, "/.git")) | |
f225aeb2 | 364 | return 0; |
6adcca3f JH |
365 | |
366 | /* | |
367 | * Otherwise it is often bare. At this point | |
368 | * we are just guessing. | |
369 | */ | |
f225aeb2 | 370 | return 1; |
6adcca3f JH |
371 | } |
372 | ||
d3af621b | 373 | static const char init_db_usage[] = |
1b1dd23f | 374 | "git init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]]"; |
d3af621b | 375 | |
4696cb93 ZW |
376 | /* |
377 | * If you want to, you can share the DB area with any number of branches. | |
378 | * That has advantages: you can save space by sharing all the SHA1 objects. | |
379 | * On the other hand, it might just make lookup slower and messier. You | |
380 | * be the judge. The default case is to have one DB per managed directory. | |
381 | */ | |
a633fca0 | 382 | int cmd_init_db(int argc, const char **argv, const char *prefix) |
e83c5163 | 383 | { |
cad88fdf | 384 | const char *git_dir; |
c3c8835f | 385 | const char *template_dir = NULL; |
f225aeb2 DB |
386 | unsigned int flags = 0; |
387 | int i; | |
e83c5163 | 388 | |
d3af621b | 389 | for (i = 1; i < argc; i++, argv++) { |
c3c8835f | 390 | const char *arg = argv[1]; |
cc44c765 | 391 | if (!prefixcmp(arg, "--template=")) |
d3af621b | 392 | template_dir = arg+11; |
74d3b23f LR |
393 | else if (!strcmp(arg, "--bare")) { |
394 | static char git_dir[PATH_MAX+1]; | |
395 | is_bare_repository_cfg = 1; | |
396 | setenv(GIT_DIR_ENVIRONMENT, getcwd(git_dir, | |
397 | sizeof(git_dir)), 0); | |
398 | } else if (!strcmp(arg, "--shared")) | |
0a2c7eea | 399 | init_shared_repository = PERM_GROUP; |
cc44c765 | 400 | else if (!prefixcmp(arg, "--shared=")) |
0a2c7eea | 401 | init_shared_repository = git_config_perm("arg", arg+9); |
4576518d | 402 | else if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet")) |
f225aeb2 | 403 | flags |= INIT_DB_QUIET; |
d3af621b | 404 | else |
8cdf3364 | 405 | usage(init_db_usage); |
d3af621b JH |
406 | } |
407 | ||
5a688fe4 JH |
408 | if (init_shared_repository != -1) |
409 | shared_repository = init_shared_repository; | |
410 | ||
6adcca3f JH |
411 | /* |
412 | * GIT_WORK_TREE makes sense only in conjunction with GIT_DIR | |
413 | * without --bare. Catch the error early. | |
414 | */ | |
415 | git_dir = getenv(GIT_DIR_ENVIRONMENT); | |
416 | if ((!git_dir || is_bare_repository_cfg == 1) | |
417 | && getenv(GIT_WORK_TREE_ENVIRONMENT)) | |
418 | die("%s (or --work-tree=<directory>) not allowed without " | |
419 | "specifying %s (or --git-dir=<directory>)", | |
420 | GIT_WORK_TREE_ENVIRONMENT, | |
421 | GIT_DIR_ENVIRONMENT); | |
422 | ||
cad88fdf LT |
423 | /* |
424 | * Set up the default .git directory contents | |
425 | */ | |
ef0a89a6 | 426 | if (!git_dir) |
cad88fdf | 427 | git_dir = DEFAULT_GIT_DIR_ENVIRONMENT; |
af6e277c | 428 | |
f225aeb2 DB |
429 | if (is_bare_repository_cfg < 0) |
430 | is_bare_repository_cfg = guess_repository_type(git_dir); | |
431 | ||
432 | if (!is_bare_repository_cfg) { | |
433 | if (git_dir) { | |
434 | const char *git_dir_parent = strrchr(git_dir, '/'); | |
435 | if (git_dir_parent) { | |
436 | char *rel = xstrndup(git_dir, git_dir_parent - git_dir); | |
437 | git_work_tree_cfg = xstrdup(make_absolute_path(rel)); | |
438 | free(rel); | |
439 | } | |
440 | } | |
441 | if (!git_work_tree_cfg) { | |
442 | git_work_tree_cfg = xcalloc(PATH_MAX, 1); | |
443 | if (!getcwd(git_work_tree_cfg, PATH_MAX)) | |
0721c314 | 444 | die_errno ("Cannot access current working directory"); |
f225aeb2 DB |
445 | } |
446 | if (access(get_git_work_tree(), X_OK)) | |
0721c314 TR |
447 | die_errno ("Cannot access work tree '%s'", |
448 | get_git_work_tree()); | |
94df2506 | 449 | } |
af6e277c | 450 | |
f225aeb2 | 451 | set_git_dir(make_absolute_path(git_dir)); |
ef0a89a6 | 452 | |
f225aeb2 | 453 | return init_db(template_dir, flags); |
e83c5163 | 454 | } |