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