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