]>
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" |
596f91ee | 9 | #include "parse-options.h" |
e83c5163 | 10 | |
d3af621b | 11 | #ifndef DEFAULT_GIT_TEMPLATE_DIR |
d52fd42a | 12 | #define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates" |
d3af621b JH |
13 | #endif |
14 | ||
c869753e SP |
15 | #ifdef NO_TRUSTABLE_FILEMODE |
16 | #define TEST_FILEMODE 0 | |
17 | #else | |
18 | #define TEST_FILEMODE 1 | |
19 | #endif | |
20 | ||
0a2c7eea DM |
21 | static int init_is_bare_repository = 0; |
22 | static int init_shared_repository = -1; | |
90b45187 | 23 | static const char *init_db_template_dir; |
b57fb80a | 24 | static const char *git_link; |
0a2c7eea | 25 | |
af6e277c | 26 | static void safe_create_dir(const char *dir, int share) |
cb126d8d | 27 | { |
f312de01 | 28 | if (mkdir(dir, 0777) < 0) { |
cb126d8d ZW |
29 | if (errno != EEXIST) { |
30 | perror(dir); | |
31 | exit(1); | |
32 | } | |
33 | } | |
af6e277c | 34 | else if (share && adjust_shared_perm(dir)) |
33e92e47 | 35 | die(_("Could not make %s writable by group"), dir); |
cb126d8d ZW |
36 | } |
37 | ||
8d5afef0 JH |
38 | static 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 | |
f18d244a | 46 | * cause "git init" to fail here. I think this is sane but |
8d5afef0 JH |
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 | */ | |
af6e277c | 51 | safe_create_dir(path, 1); |
8d5afef0 JH |
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)) | |
33e92e47 | 62 | die(_("insanely long template name %s"), de->d_name); |
8d5afef0 JH |
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) | |
33e92e47 | 67 | die_errno(_("cannot stat '%s'"), path); |
8d5afef0 JH |
68 | } |
69 | else | |
70 | exists = 1; | |
71 | ||
72 | if (lstat(template, &st_template)) | |
33e92e47 | 73 | die_errno(_("cannot stat template '%s'"), template); |
8d5afef0 JH |
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) | |
33e92e47 | 80 | die_errno(_("cannot opendir '%s'"), template); |
8d5afef0 JH |
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) | |
33e92e47 | 97 | die_errno(_("cannot readlink '%s'"), template); |
8d5afef0 | 98 | if (sizeof(lnk) <= len) |
33e92e47 | 99 | die(_("insanely long symlink %s"), template); |
8d5afef0 JH |
100 | lnk[len] = 0; |
101 | if (symlink(lnk, path)) | |
33e92e47 | 102 | die_errno(_("cannot symlink '%s' '%s'"), lnk, path); |
8d5afef0 JH |
103 | } |
104 | else if (S_ISREG(st_template.st_mode)) { | |
105 | if (copy_file(path, template, st_template.st_mode)) | |
33e92e47 | 106 | die_errno(_("cannot copy '%s' to '%s'"), template, |
0721c314 | 107 | path); |
8d5afef0 JH |
108 | } |
109 | else | |
33e92e47 | 110 | error(_("ignoring template %s"), template); |
8d5afef0 JH |
111 | } |
112 | } | |
113 | ||
f225aeb2 | 114 | static void copy_templates(const char *template_dir) |
8d5afef0 JH |
115 | { |
116 | char path[PATH_MAX]; | |
117 | char template_path[PATH_MAX]; | |
d3af621b | 118 | int template_len; |
8d5afef0 | 119 | DIR *dir; |
f225aeb2 DB |
120 | const char *git_dir = get_git_dir(); |
121 | int len = strlen(git_dir); | |
8d5afef0 | 122 | |
a47d1813 | 123 | if (!template_dir) |
d4ebc36c | 124 | template_dir = getenv(TEMPLATE_DIR_ENVIRONMENT); |
90b45187 SD |
125 | if (!template_dir) |
126 | template_dir = init_db_template_dir; | |
2de9de5e SP |
127 | if (!template_dir) |
128 | template_dir = system_path(DEFAULT_GIT_TEMPLATE_DIR); | |
172035f0 JK |
129 | if (!template_dir[0]) |
130 | return; | |
32d1776b FL |
131 | template_len = strlen(template_dir); |
132 | if (PATH_MAX <= (template_len+strlen("/config"))) | |
33e92e47 | 133 | die(_("insanely long template path %s"), template_dir); |
8d5afef0 | 134 | strcpy(template_path, template_dir); |
8d5afef0 JH |
135 | if (template_path[template_len-1] != '/') { |
136 | template_path[template_len++] = '/'; | |
137 | template_path[template_len] = 0; | |
138 | } | |
8d5afef0 | 139 | dir = opendir(template_path); |
d3af621b | 140 | if (!dir) { |
33e92e47 | 141 | warning(_("templates not found %s"), 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, | |
ef90d6d4 | 149 | template_path, NULL); |
4f629539 JH |
150 | template_path[template_len] = 0; |
151 | ||
152 | if (repository_format_version && | |
153 | repository_format_version != GIT_REPO_VERSION) { | |
33e92e47 ÆAB |
154 | warning(_("not copying templates of " |
155 | "a wrong format version %d from '%s'"), | |
4f629539 JH |
156 | repository_format_version, |
157 | template_dir); | |
158 | closedir(dir); | |
159 | return; | |
160 | } | |
161 | ||
d3af621b | 162 | memcpy(path, git_dir, len); |
f225aeb2 DB |
163 | if (len && path[len - 1] != '/') |
164 | path[len++] = '/'; | |
1f961c19 | 165 | path[len] = 0; |
8d5afef0 JH |
166 | copy_templates_1(path, len, |
167 | template_path, template_len, | |
168 | dir); | |
169 | closedir(dir); | |
170 | } | |
171 | ||
90b45187 SD |
172 | static int git_init_db_config(const char *k, const char *v, void *cb) |
173 | { | |
90b45187 SD |
174 | if (!strcmp(k, "init.templatedir")) |
175 | return git_config_pathname(&init_db_template_dir, k, v); | |
176 | ||
177 | return 0; | |
178 | } | |
179 | ||
f225aeb2 | 180 | static int create_default_files(const char *template_path) |
cad88fdf | 181 | { |
f225aeb2 | 182 | const char *git_dir = get_git_dir(); |
cad88fdf LT |
183 | unsigned len = strlen(git_dir); |
184 | static char path[PATH_MAX]; | |
4f629539 JH |
185 | struct stat st1; |
186 | char repo_version_string[10]; | |
5cc8f372 | 187 | char junk[2]; |
ef0a89a6 | 188 | int reinit; |
c869753e | 189 | int filemode; |
cad88fdf LT |
190 | |
191 | if (len > sizeof(path)-50) | |
33e92e47 | 192 | die(_("insane git directory %s"), git_dir); |
cad88fdf LT |
193 | memcpy(path, git_dir, len); |
194 | ||
195 | if (len && path[len-1] != '/') | |
196 | path[len++] = '/'; | |
197 | ||
198 | /* | |
199 | * Create .git/refs/{heads,tags} | |
200 | */ | |
f225aeb2 DB |
201 | safe_create_dir(git_path("refs"), 1); |
202 | safe_create_dir(git_path("refs/heads"), 1); | |
203 | safe_create_dir(git_path("refs/tags"), 1); | |
cad88fdf | 204 | |
90b45187 SD |
205 | /* Just look for `init.templatedir` */ |
206 | git_config(git_init_db_config, NULL); | |
207 | ||
4f629539 JH |
208 | /* First copy the templates -- we might have the default |
209 | * config file there, in which case we would want to read | |
210 | * from it after installing. | |
211 | */ | |
f225aeb2 | 212 | copy_templates(template_path); |
4f629539 | 213 | |
ef90d6d4 | 214 | git_config(git_default_config, NULL); |
0a2c7eea | 215 | is_bare_repository_cfg = init_is_bare_repository; |
5a688fe4 JH |
216 | |
217 | /* reading existing config may have overwrote it */ | |
0a2c7eea DM |
218 | if (init_shared_repository != -1) |
219 | shared_repository = init_shared_repository; | |
4f629539 | 220 | |
138086a7 JH |
221 | /* |
222 | * We would have created the above under user's umask -- under | |
223 | * shared-repository settings, we would need to fix them up. | |
224 | */ | |
225 | if (shared_repository) { | |
f225aeb2 DB |
226 | adjust_shared_perm(get_git_dir()); |
227 | adjust_shared_perm(git_path("refs")); | |
228 | adjust_shared_perm(git_path("refs/heads")); | |
229 | adjust_shared_perm(git_path("refs/tags")); | |
138086a7 JH |
230 | } |
231 | ||
cad88fdf LT |
232 | /* |
233 | * Create the default symlink from ".git/HEAD" to the "master" | |
8098a178 | 234 | * branch, if it does not exist yet. |
cad88fdf LT |
235 | */ |
236 | strcpy(path + len, "HEAD"); | |
5cc8f372 JS |
237 | reinit = (!access(path, R_OK) |
238 | || readlink(path, junk, sizeof(junk)-1) != -1); | |
ef0a89a6 | 239 | if (!reinit) { |
8b5157e4 | 240 | if (create_symref("HEAD", "refs/heads/master", NULL) < 0) |
cad88fdf | 241 | exit(1); |
cad88fdf | 242 | } |
e24317b4 | 243 | |
4f629539 JH |
244 | /* This forces creation of new config file */ |
245 | sprintf(repo_version_string, "%d", GIT_REPO_VERSION); | |
246 | git_config_set("core.repositoryformatversion", repo_version_string); | |
247 | ||
248 | path[len] = 0; | |
e24317b4 | 249 | strcpy(path + len, "config"); |
4f629539 JH |
250 | |
251 | /* Check filemode trustability */ | |
c869753e SP |
252 | filemode = TEST_FILEMODE; |
253 | if (TEST_FILEMODE && !lstat(path, &st1)) { | |
4f629539 | 254 | struct stat st2; |
c869753e | 255 | filemode = (!chmod(path, st1.st_mode ^ S_IXUSR) && |
4f629539 JH |
256 | !lstat(path, &st2) && |
257 | st1.st_mode != st2.st_mode); | |
e24317b4 | 258 | } |
c869753e | 259 | git_config_set("core.filemode", filemode ? "true" : "false"); |
ef0a89a6 | 260 | |
e90fdc39 | 261 | if (is_bare_repository()) |
7d1864ce | 262 | git_config_set("core.bare", "true"); |
7d1864ce | 263 | else { |
e90fdc39 | 264 | const char *work_tree = get_git_work_tree(); |
7d1864ce | 265 | git_config_set("core.bare", "false"); |
196055c2 AR |
266 | /* allow template config file to override the default */ |
267 | if (log_all_ref_updates == -1) | |
268 | git_config_set("core.logallrefupdates", "true"); | |
f225aeb2 DB |
269 | if (prefixcmp(git_dir, work_tree) || |
270 | strcmp(git_dir + strlen(work_tree), "/.git")) { | |
e90fdc39 | 271 | git_config_set("core.worktree", work_tree); |
f225aeb2 | 272 | } |
7d1864ce | 273 | } |
75d24499 | 274 | |
75d24499 | 275 | if (!reinit) { |
2455406a | 276 | /* Check if symlink is supported in the work tree */ |
75d24499 JH |
277 | path[len] = 0; |
278 | strcpy(path + len, "tXXXXXX"); | |
279 | if (!close(xmkstemp(path)) && | |
280 | !unlink(path) && | |
281 | !symlink("testing", path) && | |
282 | !lstat(path, &st1) && | |
283 | S_ISLNK(st1.st_mode)) | |
284 | unlink(path); /* good */ | |
285 | else | |
286 | git_config_set("core.symlinks", "false"); | |
2455406a DP |
287 | |
288 | /* Check if the filesystem is case-insensitive */ | |
289 | path[len] = 0; | |
290 | strcpy(path + len, "CoNfIg"); | |
291 | if (!access(path, F_OK)) | |
292 | git_config_set("core.ignorecase", "true"); | |
76759c7d | 293 | probe_utf8_pathname_composition(path, len); |
75d24499 JH |
294 | } |
295 | ||
ef0a89a6 | 296 | return reinit; |
cad88fdf LT |
297 | } |
298 | ||
9173912b JN |
299 | static void create_object_directory(void) |
300 | { | |
301 | const char *object_directory = get_object_directory(); | |
302 | int len = strlen(object_directory); | |
303 | char *path = xmalloc(len + 40); | |
304 | ||
305 | memcpy(path, object_directory, len); | |
306 | ||
307 | safe_create_dir(object_directory, 1); | |
308 | strcpy(path+len, "/pack"); | |
309 | safe_create_dir(path, 1); | |
310 | strcpy(path+len, "/info"); | |
311 | safe_create_dir(path, 1); | |
312 | ||
313 | free(path); | |
314 | } | |
315 | ||
b57fb80a NTND |
316 | int set_git_dir_init(const char *git_dir, const char *real_git_dir, |
317 | int exist_ok) | |
318 | { | |
319 | if (real_git_dir) { | |
320 | struct stat st; | |
321 | ||
322 | if (!exist_ok && !stat(git_dir, &st)) | |
2c050e01 | 323 | die(_("%s already exists"), git_dir); |
b57fb80a NTND |
324 | |
325 | if (!exist_ok && !stat(real_git_dir, &st)) | |
2c050e01 | 326 | die(_("%s already exists"), real_git_dir); |
b57fb80a NTND |
327 | |
328 | /* | |
329 | * make sure symlinks are resolved because we'll be | |
330 | * moving the target repo later on in separate_git_dir() | |
331 | */ | |
332 | git_link = xstrdup(real_path(git_dir)); | |
333 | } | |
334 | else { | |
335 | real_git_dir = real_path(git_dir); | |
336 | git_link = NULL; | |
337 | } | |
338 | set_git_dir(real_path(real_git_dir)); | |
339 | return 0; | |
340 | } | |
341 | ||
342 | static void separate_git_dir(const char *git_dir) | |
343 | { | |
344 | struct stat st; | |
345 | FILE *fp; | |
346 | ||
347 | if (!stat(git_link, &st)) { | |
348 | const char *src; | |
349 | ||
350 | if (S_ISREG(st.st_mode)) | |
13d6ec91 | 351 | src = read_gitfile(git_link); |
b57fb80a NTND |
352 | else if (S_ISDIR(st.st_mode)) |
353 | src = git_link; | |
354 | else | |
97f261b1 | 355 | die(_("unable to handle file type %d"), (int)st.st_mode); |
b57fb80a NTND |
356 | |
357 | if (rename(src, git_dir)) | |
2c050e01 | 358 | die_errno(_("unable to move %s to %s"), src, git_dir); |
b57fb80a NTND |
359 | } |
360 | ||
361 | fp = fopen(git_link, "w"); | |
362 | if (!fp) | |
2c050e01 | 363 | die(_("Could not create git link %s"), git_link); |
b57fb80a NTND |
364 | fprintf(fp, "gitdir: %s\n", git_dir); |
365 | fclose(fp); | |
366 | } | |
367 | ||
f225aeb2 DB |
368 | int init_db(const char *template_dir, unsigned int flags) |
369 | { | |
9173912b | 370 | int reinit; |
b57fb80a | 371 | const char *git_dir = get_git_dir(); |
f225aeb2 | 372 | |
b57fb80a NTND |
373 | if (git_link) |
374 | separate_git_dir(git_dir); | |
f225aeb2 | 375 | |
b57fb80a | 376 | safe_create_dir(git_dir, 0); |
f225aeb2 | 377 | |
0a2c7eea DM |
378 | init_is_bare_repository = is_bare_repository(); |
379 | ||
f225aeb2 DB |
380 | /* Check to see if the repository version is right. |
381 | * Note that a newly created repository does not have | |
382 | * config file, so this will not fail. What we are catching | |
383 | * is an attempt to reinitialize new repository with an old tool. | |
384 | */ | |
385 | check_repository_format(); | |
386 | ||
387 | reinit = create_default_files(template_dir); | |
388 | ||
9173912b | 389 | create_object_directory(); |
f225aeb2 DB |
390 | |
391 | if (shared_repository) { | |
392 | char buf[10]; | |
393 | /* We do not spell "group" and such, so that | |
394 | * the configuration can be read by older version | |
395 | * of git. Note, we use octal numbers for new share modes, | |
396 | * and compatibility values for PERM_GROUP and | |
397 | * PERM_EVERYBODY. | |
398 | */ | |
5a688fe4 JH |
399 | if (shared_repository < 0) |
400 | /* force to the mode value */ | |
401 | sprintf(buf, "0%o", -shared_repository); | |
402 | else if (shared_repository == PERM_GROUP) | |
f225aeb2 DB |
403 | sprintf(buf, "%d", OLD_PERM_GROUP); |
404 | else if (shared_repository == PERM_EVERYBODY) | |
405 | sprintf(buf, "%d", OLD_PERM_EVERYBODY); | |
406 | else | |
5a688fe4 | 407 | die("oops"); |
f225aeb2 DB |
408 | git_config_set("core.sharedrepository", buf); |
409 | git_config_set("receive.denyNonFastforwards", "true"); | |
410 | } | |
411 | ||
d06f15d9 | 412 | if (!(flags & INIT_DB_QUIET)) { |
d06f15d9 | 413 | int len = strlen(git_dir); |
3e5dd7e9 ÆAB |
414 | |
415 | /* | |
416 | * TRANSLATORS: The first '%s' is either "Reinitialized | |
417 | * existing" or "Initialized empty", the second " shared" or | |
418 | * "", and the last '%s%s' is the verbatim directory name. | |
419 | */ | |
420 | printf(_("%s%s Git repository in %s%s\n"), | |
421 | reinit ? _("Reinitialized existing") : _("Initialized empty"), | |
422 | shared_repository ? _(" shared") : "", | |
d06f15d9 NTND |
423 | git_dir, len && git_dir[len-1] != '/' ? "/" : ""); |
424 | } | |
f225aeb2 DB |
425 | |
426 | return 0; | |
427 | } | |
428 | ||
429 | static int guess_repository_type(const char *git_dir) | |
6adcca3f JH |
430 | { |
431 | char cwd[PATH_MAX]; | |
432 | const char *slash; | |
433 | ||
6adcca3f JH |
434 | /* |
435 | * "GIT_DIR=. git init" is always bare. | |
436 | * "GIT_DIR=`pwd` git init" too. | |
437 | */ | |
438 | if (!strcmp(".", git_dir)) | |
f225aeb2 | 439 | return 1; |
6adcca3f | 440 | if (!getcwd(cwd, sizeof(cwd))) |
33e92e47 | 441 | die_errno(_("cannot tell cwd")); |
6adcca3f | 442 | if (!strcmp(git_dir, cwd)) |
f225aeb2 | 443 | return 1; |
6adcca3f JH |
444 | /* |
445 | * "GIT_DIR=.git or GIT_DIR=something/.git is usually not. | |
446 | */ | |
447 | if (!strcmp(git_dir, ".git")) | |
f225aeb2 | 448 | return 0; |
6adcca3f JH |
449 | slash = strrchr(git_dir, '/'); |
450 | if (slash && !strcmp(slash, "/.git")) | |
f225aeb2 | 451 | return 0; |
6adcca3f JH |
452 | |
453 | /* | |
454 | * Otherwise it is often bare. At this point | |
455 | * we are just guessing. | |
456 | */ | |
f225aeb2 | 457 | return 1; |
6adcca3f JH |
458 | } |
459 | ||
596f91ee MK |
460 | static int shared_callback(const struct option *opt, const char *arg, int unset) |
461 | { | |
462 | *((int *) opt->value) = (arg) ? git_config_perm("arg", arg) : PERM_GROUP; | |
463 | return 0; | |
464 | } | |
465 | ||
466 | static const char *const init_db_usage[] = { | |
ce4a5e53 | 467 | N_("git init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]] [directory]"), |
596f91ee MK |
468 | NULL |
469 | }; | |
d3af621b | 470 | |
4696cb93 ZW |
471 | /* |
472 | * If you want to, you can share the DB area with any number of branches. | |
473 | * That has advantages: you can save space by sharing all the SHA1 objects. | |
474 | * On the other hand, it might just make lookup slower and messier. You | |
475 | * be the judge. The default case is to have one DB per managed directory. | |
476 | */ | |
a633fca0 | 477 | int cmd_init_db(int argc, const char **argv, const char *prefix) |
e83c5163 | 478 | { |
cad88fdf | 479 | const char *git_dir; |
b57fb80a | 480 | const char *real_git_dir = NULL; |
83518360 | 481 | const char *work_tree; |
c3c8835f | 482 | const char *template_dir = NULL; |
f225aeb2 | 483 | unsigned int flags = 0; |
596f91ee | 484 | const struct option init_db_options[] = { |
ce4a5e53 NTND |
485 | OPT_STRING(0, "template", &template_dir, N_("template-directory"), |
486 | N_("directory from which templates will be used")), | |
596f91ee | 487 | OPT_SET_INT(0, "bare", &is_bare_repository_cfg, |
ce4a5e53 | 488 | N_("create a bare repository"), 1), |
596f91ee | 489 | { OPTION_CALLBACK, 0, "shared", &init_shared_repository, |
ce4a5e53 NTND |
490 | N_("permissions"), |
491 | N_("specify that the git repository is to be shared amongst several users"), | |
596f91ee | 492 | PARSE_OPT_OPTARG | PARSE_OPT_NONEG, shared_callback, 0}, |
ce4a5e53 NTND |
493 | OPT_BIT('q', "quiet", &flags, N_("be quiet"), INIT_DB_QUIET), |
494 | OPT_STRING(0, "separate-git-dir", &real_git_dir, N_("gitdir"), | |
495 | N_("separate git dir from working tree")), | |
596f91ee MK |
496 | OPT_END() |
497 | }; | |
498 | ||
0397ff24 JH |
499 | argc = parse_options(argc, argv, prefix, init_db_options, init_db_usage, 0); |
500 | ||
b57fb80a NTND |
501 | if (real_git_dir && !is_absolute_path(real_git_dir)) |
502 | real_git_dir = xstrdup(real_path(real_git_dir)); | |
503 | ||
0397ff24 | 504 | if (argc == 1) { |
53d48885 NS |
505 | int mkdir_tried = 0; |
506 | retry: | |
0397ff24 | 507 | if (chdir(argv[0]) < 0) { |
53d48885 NS |
508 | if (!mkdir_tried) { |
509 | int saved; | |
510 | /* | |
511 | * At this point we haven't read any configuration, | |
512 | * and we know shared_repository should always be 0; | |
513 | * but just in case we play safe. | |
514 | */ | |
515 | saved = shared_repository; | |
516 | shared_repository = 0; | |
0397ff24 | 517 | switch (safe_create_leading_directories_const(argv[0])) { |
53d48885 NS |
518 | case -3: |
519 | errno = EEXIST; | |
520 | /* fallthru */ | |
521 | case -1: | |
33e92e47 | 522 | die_errno(_("cannot mkdir %s"), argv[0]); |
53d48885 NS |
523 | break; |
524 | default: | |
525 | break; | |
526 | } | |
527 | shared_repository = saved; | |
0397ff24 | 528 | if (mkdir(argv[0], 0777) < 0) |
33e92e47 | 529 | die_errno(_("cannot mkdir %s"), argv[0]); |
53d48885 NS |
530 | mkdir_tried = 1; |
531 | goto retry; | |
532 | } | |
33e92e47 | 533 | die_errno(_("cannot chdir to %s"), argv[0]); |
53d48885 | 534 | } |
0397ff24 JH |
535 | } else if (0 < argc) { |
536 | usage(init_db_usage[0]); | |
d3af621b | 537 | } |
0397ff24 | 538 | if (is_bare_repository_cfg == 1) { |
596f91ee | 539 | static char git_dir[PATH_MAX+1]; |
d3af621b | 540 | |
53d48885 | 541 | setenv(GIT_DIR_ENVIRONMENT, |
87a074df | 542 | getcwd(git_dir, sizeof(git_dir)), argc > 0); |
d3af621b JH |
543 | } |
544 | ||
5a688fe4 JH |
545 | if (init_shared_repository != -1) |
546 | shared_repository = init_shared_repository; | |
547 | ||
6adcca3f JH |
548 | /* |
549 | * GIT_WORK_TREE makes sense only in conjunction with GIT_DIR | |
550 | * without --bare. Catch the error early. | |
551 | */ | |
552 | git_dir = getenv(GIT_DIR_ENVIRONMENT); | |
83518360 NTND |
553 | work_tree = getenv(GIT_WORK_TREE_ENVIRONMENT); |
554 | if ((!git_dir || is_bare_repository_cfg == 1) && work_tree) | |
33e92e47 ÆAB |
555 | die(_("%s (or --work-tree=<directory>) not allowed without " |
556 | "specifying %s (or --git-dir=<directory>)"), | |
6adcca3f JH |
557 | GIT_WORK_TREE_ENVIRONMENT, |
558 | GIT_DIR_ENVIRONMENT); | |
559 | ||
cad88fdf LT |
560 | /* |
561 | * Set up the default .git directory contents | |
562 | */ | |
ef0a89a6 | 563 | if (!git_dir) |
cad88fdf | 564 | git_dir = DEFAULT_GIT_DIR_ENVIRONMENT; |
af6e277c | 565 | |
f225aeb2 DB |
566 | if (is_bare_repository_cfg < 0) |
567 | is_bare_repository_cfg = guess_repository_type(git_dir); | |
568 | ||
569 | if (!is_bare_repository_cfg) { | |
b31d2022 NTND |
570 | const char *git_dir_parent = strrchr(git_dir, '/'); |
571 | if (git_dir_parent) { | |
572 | char *rel = xstrndup(git_dir, git_dir_parent - git_dir); | |
e2a57aac | 573 | git_work_tree_cfg = xstrdup(real_path(rel)); |
b31d2022 | 574 | free(rel); |
f225aeb2 DB |
575 | } |
576 | if (!git_work_tree_cfg) { | |
577 | git_work_tree_cfg = xcalloc(PATH_MAX, 1); | |
578 | if (!getcwd(git_work_tree_cfg, PATH_MAX)) | |
33e92e47 | 579 | die_errno (_("Cannot access current working directory")); |
f225aeb2 | 580 | } |
83518360 | 581 | if (work_tree) |
e2a57aac | 582 | set_git_work_tree(real_path(work_tree)); |
83518360 NTND |
583 | else |
584 | set_git_work_tree(git_work_tree_cfg); | |
f225aeb2 | 585 | if (access(get_git_work_tree(), X_OK)) |
33e92e47 | 586 | die_errno (_("Cannot access work tree '%s'"), |
0721c314 | 587 | get_git_work_tree()); |
94df2506 | 588 | } |
83518360 NTND |
589 | else { |
590 | if (work_tree) | |
e2a57aac | 591 | set_git_work_tree(real_path(work_tree)); |
83518360 | 592 | } |
af6e277c | 593 | |
b57fb80a | 594 | set_git_dir_init(git_dir, real_git_dir, 1); |
ef0a89a6 | 595 | |
f225aeb2 | 596 | return init_db(template_dir, flags); |
e83c5163 | 597 | } |