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