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