]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/init-db.c
copy.h: move declarations for copy.c functions from cache.h
[thirdparty/git.git] / builtin / init-db.c
1 /*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "cache.h"
7 #include "abspath.h"
8 #include "config.h"
9 #include "copy.h"
10 #include "environment.h"
11 #include "gettext.h"
12 #include "refs.h"
13 #include "builtin.h"
14 #include "exec-cmd.h"
15 #include "object-file.h"
16 #include "parse-options.h"
17 #include "setup.h"
18 #include "worktree.h"
19 #include "wrapper.h"
20
21 #ifndef DEFAULT_GIT_TEMPLATE_DIR
22 #define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates"
23 #endif
24
25 #ifdef NO_TRUSTABLE_FILEMODE
26 #define TEST_FILEMODE 0
27 #else
28 #define TEST_FILEMODE 1
29 #endif
30
31 #define GIT_DEFAULT_HASH_ENVIRONMENT "GIT_DEFAULT_HASH"
32
33 static int init_is_bare_repository = 0;
34 static int init_shared_repository = -1;
35
36 static void copy_templates_1(struct strbuf *path, struct strbuf *template_path,
37 DIR *dir)
38 {
39 size_t path_baselen = path->len;
40 size_t template_baselen = template_path->len;
41 struct dirent *de;
42
43 /* Note: if ".git/hooks" file exists in the repository being
44 * re-initialized, /etc/core-git/templates/hooks/update would
45 * cause "git init" to fail here. I think this is sane but
46 * it means that the set of templates we ship by default, along
47 * with the way the namespace under .git/ is organized, should
48 * be really carefully chosen.
49 */
50 safe_create_dir(path->buf, 1);
51 while ((de = readdir(dir)) != NULL) {
52 struct stat st_git, st_template;
53 int exists = 0;
54
55 strbuf_setlen(path, path_baselen);
56 strbuf_setlen(template_path, template_baselen);
57
58 if (de->d_name[0] == '.')
59 continue;
60 strbuf_addstr(path, de->d_name);
61 strbuf_addstr(template_path, de->d_name);
62 if (lstat(path->buf, &st_git)) {
63 if (errno != ENOENT)
64 die_errno(_("cannot stat '%s'"), path->buf);
65 }
66 else
67 exists = 1;
68
69 if (lstat(template_path->buf, &st_template))
70 die_errno(_("cannot stat template '%s'"), template_path->buf);
71
72 if (S_ISDIR(st_template.st_mode)) {
73 DIR *subdir = opendir(template_path->buf);
74 if (!subdir)
75 die_errno(_("cannot opendir '%s'"), template_path->buf);
76 strbuf_addch(path, '/');
77 strbuf_addch(template_path, '/');
78 copy_templates_1(path, template_path, subdir);
79 closedir(subdir);
80 }
81 else if (exists)
82 continue;
83 else if (S_ISLNK(st_template.st_mode)) {
84 struct strbuf lnk = STRBUF_INIT;
85 if (strbuf_readlink(&lnk, template_path->buf,
86 st_template.st_size) < 0)
87 die_errno(_("cannot readlink '%s'"), template_path->buf);
88 if (symlink(lnk.buf, path->buf))
89 die_errno(_("cannot symlink '%s' '%s'"),
90 lnk.buf, path->buf);
91 strbuf_release(&lnk);
92 }
93 else if (S_ISREG(st_template.st_mode)) {
94 if (copy_file(path->buf, template_path->buf, st_template.st_mode))
95 die_errno(_("cannot copy '%s' to '%s'"),
96 template_path->buf, path->buf);
97 }
98 else
99 error(_("ignoring template %s"), template_path->buf);
100 }
101 }
102
103 static void copy_templates(const char *template_dir, const char *init_template_dir)
104 {
105 struct strbuf path = STRBUF_INIT;
106 struct strbuf template_path = STRBUF_INIT;
107 size_t template_len;
108 struct repository_format template_format = REPOSITORY_FORMAT_INIT;
109 struct strbuf err = STRBUF_INIT;
110 DIR *dir;
111 char *to_free = NULL;
112
113 if (!template_dir)
114 template_dir = getenv(TEMPLATE_DIR_ENVIRONMENT);
115 if (!template_dir)
116 template_dir = init_template_dir;
117 if (!template_dir)
118 template_dir = to_free = system_path(DEFAULT_GIT_TEMPLATE_DIR);
119 if (!template_dir[0]) {
120 free(to_free);
121 return;
122 }
123
124 strbuf_addstr(&template_path, template_dir);
125 strbuf_complete(&template_path, '/');
126 template_len = template_path.len;
127
128 dir = opendir(template_path.buf);
129 if (!dir) {
130 warning(_("templates not found in %s"), template_dir);
131 goto free_return;
132 }
133
134 /* Make sure that template is from the correct vintage */
135 strbuf_addstr(&template_path, "config");
136 read_repository_format(&template_format, template_path.buf);
137 strbuf_setlen(&template_path, template_len);
138
139 /*
140 * No mention of version at all is OK, but anything else should be
141 * verified.
142 */
143 if (template_format.version >= 0 &&
144 verify_repository_format(&template_format, &err) < 0) {
145 warning(_("not copying templates from '%s': %s"),
146 template_dir, err.buf);
147 strbuf_release(&err);
148 goto close_free_return;
149 }
150
151 strbuf_addstr(&path, get_git_common_dir());
152 strbuf_complete(&path, '/');
153 copy_templates_1(&path, &template_path, dir);
154 close_free_return:
155 closedir(dir);
156 free_return:
157 free(to_free);
158 strbuf_release(&path);
159 strbuf_release(&template_path);
160 clear_repository_format(&template_format);
161 }
162
163 /*
164 * If the git_dir is not directly inside the working tree, then git will not
165 * find it by default, and we need to set the worktree explicitly.
166 */
167 static int needs_work_tree_config(const char *git_dir, const char *work_tree)
168 {
169 if (!strcmp(work_tree, "/") && !strcmp(git_dir, "/.git"))
170 return 0;
171 if (skip_prefix(git_dir, work_tree, &git_dir) &&
172 !strcmp(git_dir, "/.git"))
173 return 0;
174 return 1;
175 }
176
177 void initialize_repository_version(int hash_algo, int reinit)
178 {
179 char repo_version_string[10];
180 int repo_version = GIT_REPO_VERSION;
181
182 if (hash_algo != GIT_HASH_SHA1)
183 repo_version = GIT_REPO_VERSION_READ;
184
185 /* This forces creation of new config file */
186 xsnprintf(repo_version_string, sizeof(repo_version_string),
187 "%d", repo_version);
188 git_config_set("core.repositoryformatversion", repo_version_string);
189
190 if (hash_algo != GIT_HASH_SHA1)
191 git_config_set("extensions.objectformat",
192 hash_algos[hash_algo].name);
193 else if (reinit)
194 git_config_set_gently("extensions.objectformat", NULL);
195 }
196
197 static int create_default_files(const char *template_path,
198 const char *original_git_dir,
199 const char *initial_branch,
200 const struct repository_format *fmt,
201 int quiet)
202 {
203 struct stat st1;
204 struct strbuf buf = STRBUF_INIT;
205 char *path;
206 char junk[2];
207 int reinit;
208 int filemode;
209 struct strbuf err = STRBUF_INIT;
210 const char *init_template_dir = NULL;
211 const char *work_tree = get_git_work_tree();
212
213 /*
214 * First copy the templates -- we might have the default
215 * config file there, in which case we would want to read
216 * from it after installing.
217 *
218 * Before reading that config, we also need to clear out any cached
219 * values (since we've just potentially changed what's available on
220 * disk).
221 */
222 git_config_get_pathname("init.templatedir", &init_template_dir);
223 copy_templates(template_path, init_template_dir);
224 free((char *)init_template_dir);
225 git_config_clear();
226 reset_shared_repository();
227 git_config(git_default_config, NULL);
228
229 /*
230 * We must make sure command-line options continue to override any
231 * values we might have just re-read from the config.
232 */
233 is_bare_repository_cfg = init_is_bare_repository || !work_tree;
234 if (init_shared_repository != -1)
235 set_shared_repository(init_shared_repository);
236
237 /*
238 * We would have created the above under user's umask -- under
239 * shared-repository settings, we would need to fix them up.
240 */
241 if (get_shared_repository()) {
242 adjust_shared_perm(get_git_dir());
243 }
244
245 /*
246 * We need to create a "refs" dir in any case so that older
247 * versions of git can tell that this is a repository.
248 */
249 safe_create_dir(git_path("refs"), 1);
250 adjust_shared_perm(git_path("refs"));
251
252 if (refs_init_db(&err))
253 die("failed to set up refs db: %s", err.buf);
254
255 /*
256 * Point the HEAD symref to the initial branch with if HEAD does
257 * not yet exist.
258 */
259 path = git_path_buf(&buf, "HEAD");
260 reinit = (!access(path, R_OK)
261 || readlink(path, junk, sizeof(junk)-1) != -1);
262 if (!reinit) {
263 char *ref;
264
265 if (!initial_branch)
266 initial_branch = git_default_branch_name(quiet);
267
268 ref = xstrfmt("refs/heads/%s", initial_branch);
269 if (check_refname_format(ref, 0) < 0)
270 die(_("invalid initial branch name: '%s'"),
271 initial_branch);
272
273 if (create_symref("HEAD", ref, NULL) < 0)
274 exit(1);
275 free(ref);
276 }
277
278 initialize_repository_version(fmt->hash_algo, 0);
279
280 /* Check filemode trustability */
281 path = git_path_buf(&buf, "config");
282 filemode = TEST_FILEMODE;
283 if (TEST_FILEMODE && !lstat(path, &st1)) {
284 struct stat st2;
285 filemode = (!chmod(path, st1.st_mode ^ S_IXUSR) &&
286 !lstat(path, &st2) &&
287 st1.st_mode != st2.st_mode &&
288 !chmod(path, st1.st_mode));
289 if (filemode && !reinit && (st1.st_mode & S_IXUSR))
290 filemode = 0;
291 }
292 git_config_set("core.filemode", filemode ? "true" : "false");
293
294 if (is_bare_repository())
295 git_config_set("core.bare", "true");
296 else {
297 git_config_set("core.bare", "false");
298 /* allow template config file to override the default */
299 if (log_all_ref_updates == LOG_REFS_UNSET)
300 git_config_set("core.logallrefupdates", "true");
301 if (needs_work_tree_config(original_git_dir, work_tree))
302 git_config_set("core.worktree", work_tree);
303 }
304
305 if (!reinit) {
306 /* Check if symlink is supported in the work tree */
307 path = git_path_buf(&buf, "tXXXXXX");
308 if (!close(xmkstemp(path)) &&
309 !unlink(path) &&
310 !symlink("testing", path) &&
311 !lstat(path, &st1) &&
312 S_ISLNK(st1.st_mode))
313 unlink(path); /* good */
314 else
315 git_config_set("core.symlinks", "false");
316
317 /* Check if the filesystem is case-insensitive */
318 path = git_path_buf(&buf, "CoNfIg");
319 if (!access(path, F_OK))
320 git_config_set("core.ignorecase", "true");
321 probe_utf8_pathname_composition();
322 }
323
324 strbuf_release(&buf);
325 return reinit;
326 }
327
328 static void create_object_directory(void)
329 {
330 struct strbuf path = STRBUF_INIT;
331 size_t baselen;
332
333 strbuf_addstr(&path, get_object_directory());
334 baselen = path.len;
335
336 safe_create_dir(path.buf, 1);
337
338 strbuf_setlen(&path, baselen);
339 strbuf_addstr(&path, "/pack");
340 safe_create_dir(path.buf, 1);
341
342 strbuf_setlen(&path, baselen);
343 strbuf_addstr(&path, "/info");
344 safe_create_dir(path.buf, 1);
345
346 strbuf_release(&path);
347 }
348
349 static void separate_git_dir(const char *git_dir, const char *git_link)
350 {
351 struct stat st;
352
353 if (!stat(git_link, &st)) {
354 const char *src;
355
356 if (S_ISREG(st.st_mode))
357 src = read_gitfile(git_link);
358 else if (S_ISDIR(st.st_mode))
359 src = git_link;
360 else
361 die(_("unable to handle file type %d"), (int)st.st_mode);
362
363 if (rename(src, git_dir))
364 die_errno(_("unable to move %s to %s"), src, git_dir);
365 repair_worktrees(NULL, NULL);
366 }
367
368 write_file(git_link, "gitdir: %s", git_dir);
369 }
370
371 static void validate_hash_algorithm(struct repository_format *repo_fmt, int hash)
372 {
373 const char *env = getenv(GIT_DEFAULT_HASH_ENVIRONMENT);
374 /*
375 * If we already have an initialized repo, don't allow the user to
376 * specify a different algorithm, as that could cause corruption.
377 * Otherwise, if the user has specified one on the command line, use it.
378 */
379 if (repo_fmt->version >= 0 && hash != GIT_HASH_UNKNOWN && hash != repo_fmt->hash_algo)
380 die(_("attempt to reinitialize repository with different hash"));
381 else if (hash != GIT_HASH_UNKNOWN)
382 repo_fmt->hash_algo = hash;
383 else if (env) {
384 int env_algo = hash_algo_by_name(env);
385 if (env_algo == GIT_HASH_UNKNOWN)
386 die(_("unknown hash algorithm '%s'"), env);
387 repo_fmt->hash_algo = env_algo;
388 }
389 }
390
391 int init_db(const char *git_dir, const char *real_git_dir,
392 const char *template_dir, int hash, const char *initial_branch,
393 unsigned int flags)
394 {
395 int reinit;
396 int exist_ok = flags & INIT_DB_EXIST_OK;
397 char *original_git_dir = real_pathdup(git_dir, 1);
398 struct repository_format repo_fmt = REPOSITORY_FORMAT_INIT;
399
400 if (real_git_dir) {
401 struct stat st;
402
403 if (!exist_ok && !stat(git_dir, &st))
404 die(_("%s already exists"), git_dir);
405
406 if (!exist_ok && !stat(real_git_dir, &st))
407 die(_("%s already exists"), real_git_dir);
408
409 set_git_dir(real_git_dir, 1);
410 git_dir = get_git_dir();
411 separate_git_dir(git_dir, original_git_dir);
412 }
413 else {
414 set_git_dir(git_dir, 1);
415 git_dir = get_git_dir();
416 }
417 startup_info->have_repository = 1;
418
419 /* Ensure `core.hidedotfiles` is processed */
420 git_config(platform_core_config, NULL);
421
422 safe_create_dir(git_dir, 0);
423
424 init_is_bare_repository = is_bare_repository();
425
426 /* Check to see if the repository version is right.
427 * Note that a newly created repository does not have
428 * config file, so this will not fail. What we are catching
429 * is an attempt to reinitialize new repository with an old tool.
430 */
431 check_repository_format(&repo_fmt);
432
433 validate_hash_algorithm(&repo_fmt, hash);
434
435 reinit = create_default_files(template_dir, original_git_dir,
436 initial_branch, &repo_fmt,
437 flags & INIT_DB_QUIET);
438 if (reinit && initial_branch)
439 warning(_("re-init: ignored --initial-branch=%s"),
440 initial_branch);
441
442 create_object_directory();
443
444 if (get_shared_repository()) {
445 char buf[10];
446 /* We do not spell "group" and such, so that
447 * the configuration can be read by older version
448 * of git. Note, we use octal numbers for new share modes,
449 * and compatibility values for PERM_GROUP and
450 * PERM_EVERYBODY.
451 */
452 if (get_shared_repository() < 0)
453 /* force to the mode value */
454 xsnprintf(buf, sizeof(buf), "0%o", -get_shared_repository());
455 else if (get_shared_repository() == PERM_GROUP)
456 xsnprintf(buf, sizeof(buf), "%d", OLD_PERM_GROUP);
457 else if (get_shared_repository() == PERM_EVERYBODY)
458 xsnprintf(buf, sizeof(buf), "%d", OLD_PERM_EVERYBODY);
459 else
460 BUG("invalid value for shared_repository");
461 git_config_set("core.sharedrepository", buf);
462 git_config_set("receive.denyNonFastforwards", "true");
463 }
464
465 if (!(flags & INIT_DB_QUIET)) {
466 int len = strlen(git_dir);
467
468 if (reinit)
469 printf(get_shared_repository()
470 ? _("Reinitialized existing shared Git repository in %s%s\n")
471 : _("Reinitialized existing Git repository in %s%s\n"),
472 git_dir, len && git_dir[len-1] != '/' ? "/" : "");
473 else
474 printf(get_shared_repository()
475 ? _("Initialized empty shared Git repository in %s%s\n")
476 : _("Initialized empty Git repository in %s%s\n"),
477 git_dir, len && git_dir[len-1] != '/' ? "/" : "");
478 }
479
480 free(original_git_dir);
481 return 0;
482 }
483
484 static int guess_repository_type(const char *git_dir)
485 {
486 const char *slash;
487 char *cwd;
488 int cwd_is_git_dir;
489
490 /*
491 * "GIT_DIR=. git init" is always bare.
492 * "GIT_DIR=`pwd` git init" too.
493 */
494 if (!strcmp(".", git_dir))
495 return 1;
496 cwd = xgetcwd();
497 cwd_is_git_dir = !strcmp(git_dir, cwd);
498 free(cwd);
499 if (cwd_is_git_dir)
500 return 1;
501 /*
502 * "GIT_DIR=.git or GIT_DIR=something/.git is usually not.
503 */
504 if (!strcmp(git_dir, ".git"))
505 return 0;
506 slash = strrchr(git_dir, '/');
507 if (slash && !strcmp(slash, "/.git"))
508 return 0;
509
510 /*
511 * Otherwise it is often bare. At this point
512 * we are just guessing.
513 */
514 return 1;
515 }
516
517 static int shared_callback(const struct option *opt, const char *arg, int unset)
518 {
519 BUG_ON_OPT_NEG(unset);
520 *((int *) opt->value) = (arg) ? git_config_perm("arg", arg) : PERM_GROUP;
521 return 0;
522 }
523
524 static const char *const init_db_usage[] = {
525 N_("git init [-q | --quiet] [--bare] [--template=<template-directory>]\n"
526 " [--separate-git-dir <git-dir>] [--object-format=<format>]\n"
527 " [-b <branch-name> | --initial-branch=<branch-name>]\n"
528 " [--shared[=<permissions>]] [<directory>]"),
529 NULL
530 };
531
532 /*
533 * If you want to, you can share the DB area with any number of branches.
534 * That has advantages: you can save space by sharing all the SHA1 objects.
535 * On the other hand, it might just make lookup slower and messier. You
536 * be the judge. The default case is to have one DB per managed directory.
537 */
538 int cmd_init_db(int argc, const char **argv, const char *prefix)
539 {
540 const char *git_dir;
541 const char *real_git_dir = NULL;
542 const char *work_tree;
543 const char *template_dir = NULL;
544 unsigned int flags = 0;
545 const char *object_format = NULL;
546 const char *initial_branch = NULL;
547 int hash_algo = GIT_HASH_UNKNOWN;
548 const struct option init_db_options[] = {
549 OPT_STRING(0, "template", &template_dir, N_("template-directory"),
550 N_("directory from which templates will be used")),
551 OPT_SET_INT(0, "bare", &is_bare_repository_cfg,
552 N_("create a bare repository"), 1),
553 { OPTION_CALLBACK, 0, "shared", &init_shared_repository,
554 N_("permissions"),
555 N_("specify that the git repository is to be shared amongst several users"),
556 PARSE_OPT_OPTARG | PARSE_OPT_NONEG, shared_callback, 0},
557 OPT_BIT('q', "quiet", &flags, N_("be quiet"), INIT_DB_QUIET),
558 OPT_STRING(0, "separate-git-dir", &real_git_dir, N_("gitdir"),
559 N_("separate git dir from working tree")),
560 OPT_STRING('b', "initial-branch", &initial_branch, N_("name"),
561 N_("override the name of the initial branch")),
562 OPT_STRING(0, "object-format", &object_format, N_("hash"),
563 N_("specify the hash algorithm to use")),
564 OPT_END()
565 };
566
567 argc = parse_options(argc, argv, prefix, init_db_options, init_db_usage, 0);
568
569 if (real_git_dir && is_bare_repository_cfg == 1)
570 die(_("options '%s' and '%s' cannot be used together"), "--separate-git-dir", "--bare");
571
572 if (real_git_dir && !is_absolute_path(real_git_dir))
573 real_git_dir = real_pathdup(real_git_dir, 1);
574
575 if (template_dir && *template_dir && !is_absolute_path(template_dir)) {
576 template_dir = absolute_pathdup(template_dir);
577 UNLEAK(template_dir);
578 }
579
580 if (argc == 1) {
581 int mkdir_tried = 0;
582 retry:
583 if (chdir(argv[0]) < 0) {
584 if (!mkdir_tried) {
585 int saved;
586 /*
587 * At this point we haven't read any configuration,
588 * and we know shared_repository should always be 0;
589 * but just in case we play safe.
590 */
591 saved = get_shared_repository();
592 set_shared_repository(0);
593 switch (safe_create_leading_directories_const(argv[0])) {
594 case SCLD_OK:
595 case SCLD_PERMS:
596 break;
597 case SCLD_EXISTS:
598 errno = EEXIST;
599 /* fallthru */
600 default:
601 die_errno(_("cannot mkdir %s"), argv[0]);
602 break;
603 }
604 set_shared_repository(saved);
605 if (mkdir(argv[0], 0777) < 0)
606 die_errno(_("cannot mkdir %s"), argv[0]);
607 mkdir_tried = 1;
608 goto retry;
609 }
610 die_errno(_("cannot chdir to %s"), argv[0]);
611 }
612 } else if (0 < argc) {
613 usage(init_db_usage[0]);
614 }
615 if (is_bare_repository_cfg == 1) {
616 char *cwd = xgetcwd();
617 setenv(GIT_DIR_ENVIRONMENT, cwd, argc > 0);
618 free(cwd);
619 }
620
621 if (object_format) {
622 hash_algo = hash_algo_by_name(object_format);
623 if (hash_algo == GIT_HASH_UNKNOWN)
624 die(_("unknown hash algorithm '%s'"), object_format);
625 }
626
627 if (init_shared_repository != -1)
628 set_shared_repository(init_shared_repository);
629
630 /*
631 * GIT_WORK_TREE makes sense only in conjunction with GIT_DIR
632 * without --bare. Catch the error early.
633 */
634 git_dir = xstrdup_or_null(getenv(GIT_DIR_ENVIRONMENT));
635 work_tree = xstrdup_or_null(getenv(GIT_WORK_TREE_ENVIRONMENT));
636 if ((!git_dir || is_bare_repository_cfg == 1) && work_tree)
637 die(_("%s (or --work-tree=<directory>) not allowed without "
638 "specifying %s (or --git-dir=<directory>)"),
639 GIT_WORK_TREE_ENVIRONMENT,
640 GIT_DIR_ENVIRONMENT);
641
642 /*
643 * Set up the default .git directory contents
644 */
645 if (!git_dir)
646 git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
647
648 /*
649 * When --separate-git-dir is used inside a linked worktree, take
650 * care to ensure that the common .git/ directory is relocated, not
651 * the worktree-specific .git/worktrees/<id>/ directory.
652 */
653 if (real_git_dir) {
654 int err;
655 const char *p;
656 struct strbuf sb = STRBUF_INIT;
657
658 p = read_gitfile_gently(git_dir, &err);
659 if (p && get_common_dir(&sb, p)) {
660 struct strbuf mainwt = STRBUF_INIT;
661
662 strbuf_addbuf(&mainwt, &sb);
663 strbuf_strip_suffix(&mainwt, "/.git");
664 if (chdir(mainwt.buf) < 0)
665 die_errno(_("cannot chdir to %s"), mainwt.buf);
666 strbuf_release(&mainwt);
667 git_dir = strbuf_detach(&sb, NULL);
668 }
669 strbuf_release(&sb);
670 }
671
672 if (is_bare_repository_cfg < 0)
673 is_bare_repository_cfg = guess_repository_type(git_dir);
674
675 if (!is_bare_repository_cfg) {
676 const char *git_dir_parent = strrchr(git_dir, '/');
677 if (git_dir_parent) {
678 char *rel = xstrndup(git_dir, git_dir_parent - git_dir);
679 git_work_tree_cfg = real_pathdup(rel, 1);
680 free(rel);
681 }
682 if (!git_work_tree_cfg)
683 git_work_tree_cfg = xgetcwd();
684 if (work_tree)
685 set_git_work_tree(work_tree);
686 else
687 set_git_work_tree(git_work_tree_cfg);
688 if (access(get_git_work_tree(), X_OK))
689 die_errno (_("Cannot access work tree '%s'"),
690 get_git_work_tree());
691 }
692 else {
693 if (real_git_dir)
694 die(_("--separate-git-dir incompatible with bare repository"));
695 if (work_tree)
696 set_git_work_tree(work_tree);
697 }
698
699 UNLEAK(real_git_dir);
700 UNLEAK(git_dir);
701 UNLEAK(work_tree);
702
703 flags |= INIT_DB_EXIST_OK;
704 return init_db(git_dir, real_git_dir, template_dir, hash_algo,
705 initial_branch, flags);
706 }