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