]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/init-db.c
Merge branch 'kd/t0028-octal-del-is-377-not-777'
[thirdparty/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"
b2141fc1 7#include "config.h"
fb58c8d5 8#include "refs.h"
c3c8835f 9#include "builtin.h"
d807c4a0 10#include "exec-cmd.h"
596f91ee 11#include "parse-options.h"
e83c5163 12
d3af621b 13#ifndef DEFAULT_GIT_TEMPLATE_DIR
d52fd42a 14#define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates"
d3af621b
JH
15#endif
16
c869753e
SP
17#ifdef NO_TRUSTABLE_FILEMODE
18#define TEST_FILEMODE 0
19#else
20#define TEST_FILEMODE 1
21#endif
22
0a2c7eea
DM
23static int init_is_bare_repository = 0;
24static int init_shared_repository = -1;
90b45187 25static const char *init_db_template_dir;
0a2c7eea 26
fa0fccae 27static void copy_templates_1(struct strbuf *path, struct strbuf *template_path,
8d5afef0
JH
28 DIR *dir)
29{
9c28390b 30 size_t path_baselen = path->len;
fa0fccae 31 size_t template_baselen = template_path->len;
8d5afef0
JH
32 struct dirent *de;
33
34 /* Note: if ".git/hooks" file exists in the repository being
35 * re-initialized, /etc/core-git/templates/hooks/update would
f18d244a 36 * cause "git init" to fail here. I think this is sane but
8d5afef0
JH
37 * it means that the set of templates we ship by default, along
38 * with the way the namespace under .git/ is organized, should
39 * be really carefully chosen.
40 */
9c28390b 41 safe_create_dir(path->buf, 1);
8d5afef0
JH
42 while ((de = readdir(dir)) != NULL) {
43 struct stat st_git, st_template;
8d5afef0
JH
44 int exists = 0;
45
9c28390b 46 strbuf_setlen(path, path_baselen);
fa0fccae 47 strbuf_setlen(template_path, template_baselen);
9c28390b 48
8d5afef0
JH
49 if (de->d_name[0] == '.')
50 continue;
9c28390b 51 strbuf_addstr(path, de->d_name);
fa0fccae 52 strbuf_addstr(template_path, de->d_name);
9c28390b 53 if (lstat(path->buf, &st_git)) {
8d5afef0 54 if (errno != ENOENT)
9c28390b 55 die_errno(_("cannot stat '%s'"), path->buf);
8d5afef0
JH
56 }
57 else
58 exists = 1;
59
fa0fccae
BW
60 if (lstat(template_path->buf, &st_template))
61 die_errno(_("cannot stat template '%s'"), template_path->buf);
8d5afef0
JH
62
63 if (S_ISDIR(st_template.st_mode)) {
fa0fccae 64 DIR *subdir = opendir(template_path->buf);
8d5afef0 65 if (!subdir)
fa0fccae 66 die_errno(_("cannot opendir '%s'"), template_path->buf);
9c28390b 67 strbuf_addch(path, '/');
fa0fccae
BW
68 strbuf_addch(template_path, '/');
69 copy_templates_1(path, template_path, subdir);
8d5afef0
JH
70 closedir(subdir);
71 }
72 else if (exists)
73 continue;
74 else if (S_ISLNK(st_template.st_mode)) {
9c28390b 75 struct strbuf lnk = STRBUF_INIT;
765b496d
JK
76 if (strbuf_readlink(&lnk, template_path->buf,
77 st_template.st_size) < 0)
fa0fccae 78 die_errno(_("cannot readlink '%s'"), template_path->buf);
9c28390b
JK
79 if (symlink(lnk.buf, path->buf))
80 die_errno(_("cannot symlink '%s' '%s'"),
81 lnk.buf, path->buf);
82 strbuf_release(&lnk);
8d5afef0
JH
83 }
84 else if (S_ISREG(st_template.st_mode)) {
fa0fccae 85 if (copy_file(path->buf, template_path->buf, st_template.st_mode))
9c28390b 86 die_errno(_("cannot copy '%s' to '%s'"),
fa0fccae 87 template_path->buf, path->buf);
8d5afef0
JH
88 }
89 else
fa0fccae 90 error(_("ignoring template %s"), template_path->buf);
8d5afef0
JH
91 }
92}
93
f225aeb2 94static void copy_templates(const char *template_dir)
8d5afef0 95{
9c28390b
JK
96 struct strbuf path = STRBUF_INIT;
97 struct strbuf template_path = STRBUF_INIT;
98 size_t template_len;
94ce1672
JK
99 struct repository_format template_format;
100 struct strbuf err = STRBUF_INIT;
8d5afef0 101 DIR *dir;
59362e56 102 char *to_free = NULL;
8d5afef0 103
a47d1813 104 if (!template_dir)
d4ebc36c 105 template_dir = getenv(TEMPLATE_DIR_ENVIRONMENT);
90b45187
SD
106 if (!template_dir)
107 template_dir = init_db_template_dir;
2de9de5e 108 if (!template_dir)
59362e56
JH
109 template_dir = to_free = system_path(DEFAULT_GIT_TEMPLATE_DIR);
110 if (!template_dir[0]) {
111 free(to_free);
172035f0 112 return;
59362e56 113 }
9c28390b
JK
114
115 strbuf_addstr(&template_path, template_dir);
116 strbuf_complete(&template_path, '/');
117 template_len = template_path.len;
118
119 dir = opendir(template_path.buf);
d3af621b 120 if (!dir) {
44f560fc 121 warning(_("templates not found in %s"), template_dir);
59362e56 122 goto free_return;
d3af621b
JH
123 }
124
4f629539 125 /* Make sure that template is from the correct vintage */
9c28390b 126 strbuf_addstr(&template_path, "config");
94ce1672 127 read_repository_format(&template_format, template_path.buf);
9c28390b 128 strbuf_setlen(&template_path, template_len);
4f629539 129
94ce1672
JK
130 /*
131 * No mention of version at all is OK, but anything else should be
132 * verified.
133 */
134 if (template_format.version >= 0 &&
135 verify_repository_format(&template_format, &err) < 0) {
136 warning(_("not copying templates from '%s': %s"),
137 template_dir, err.buf);
138 strbuf_release(&err);
59362e56 139 goto close_free_return;
4f629539
JH
140 }
141
fe9aa0b2 142 strbuf_addstr(&path, get_git_common_dir());
9c28390b
JK
143 strbuf_complete(&path, '/');
144 copy_templates_1(&path, &template_path, dir);
59362e56 145close_free_return:
8d5afef0 146 closedir(dir);
59362e56
JH
147free_return:
148 free(to_free);
9c28390b
JK
149 strbuf_release(&path);
150 strbuf_release(&template_path);
8d5afef0
JH
151}
152
90b45187
SD
153static int git_init_db_config(const char *k, const char *v, void *cb)
154{
90b45187
SD
155 if (!strcmp(k, "init.templatedir"))
156 return git_config_pathname(&init_db_template_dir, k, v);
157
158 return 0;
159}
160
84ccad8d
JK
161/*
162 * If the git_dir is not directly inside the working tree, then git will not
163 * find it by default, and we need to set the worktree explicitly.
164 */
165static int needs_work_tree_config(const char *git_dir, const char *work_tree)
166{
167 if (!strcmp(work_tree, "/") && !strcmp(git_dir, "/.git"))
168 return 0;
169 if (skip_prefix(git_dir, work_tree, &git_dir) &&
170 !strcmp(git_dir, "/.git"))
171 return 0;
172 return 1;
173}
174
6311cfaf
NTND
175static int create_default_files(const char *template_path,
176 const char *original_git_dir)
cad88fdf 177{
4f629539 178 struct stat st1;
9c28390b
JK
179 struct strbuf buf = STRBUF_INIT;
180 char *path;
4f629539 181 char repo_version_string[10];
5cc8f372 182 char junk[2];
ef0a89a6 183 int reinit;
c869753e 184 int filemode;
6fb5acfd 185 struct strbuf err = STRBUF_INIT;
cad88fdf 186
90b45187
SD
187 /* Just look for `init.templatedir` */
188 git_config(git_init_db_config, NULL);
189
7c0a842b
JK
190 /*
191 * First copy the templates -- we might have the default
4f629539
JH
192 * config file there, in which case we would want to read
193 * from it after installing.
4543926b
JK
194 *
195 * Before reading that config, we also need to clear out any cached
196 * values (since we've just potentially changed what's available on
197 * disk).
4f629539 198 */
f225aeb2 199 copy_templates(template_path);
4543926b
JK
200 git_config_clear();
201 reset_shared_repository();
ef90d6d4 202 git_config(git_default_config, NULL);
5a688fe4 203
7c0a842b
JK
204 /*
205 * We must make sure command-line options continue to override any
206 * values we might have just re-read from the config.
207 */
208 is_bare_repository_cfg = init_is_bare_repository;
0a2c7eea 209 if (init_shared_repository != -1)
7875acb6 210 set_shared_repository(init_shared_repository);
4f629539 211
138086a7
JH
212 /*
213 * We would have created the above under user's umask -- under
214 * shared-repository settings, we would need to fix them up.
215 */
7875acb6 216 if (get_shared_repository()) {
f225aeb2 217 adjust_shared_perm(get_git_dir());
138086a7
JH
218 }
219
6fb5acfd
DT
220 /*
221 * We need to create a "refs" dir in any case so that older
222 * versions of git can tell that this is a repository.
223 */
224 safe_create_dir(git_path("refs"), 1);
225 adjust_shared_perm(git_path("refs"));
226
227 if (refs_init_db(&err))
228 die("failed to set up refs db: %s", err.buf);
229
cad88fdf
LT
230 /*
231 * Create the default symlink from ".git/HEAD" to the "master"
8098a178 232 * branch, if it does not exist yet.
cad88fdf 233 */
9c28390b 234 path = git_path_buf(&buf, "HEAD");
5cc8f372
JS
235 reinit = (!access(path, R_OK)
236 || readlink(path, junk, sizeof(junk)-1) != -1);
ef0a89a6 237 if (!reinit) {
8b5157e4 238 if (create_symref("HEAD", "refs/heads/master", NULL) < 0)
cad88fdf 239 exit(1);
cad88fdf 240 }
e24317b4 241
4f629539 242 /* This forces creation of new config file */
5096d490
JK
243 xsnprintf(repo_version_string, sizeof(repo_version_string),
244 "%d", GIT_REPO_VERSION);
3d180648 245 git_config_set("core.repositoryformatversion", repo_version_string);
4f629539 246
4f629539 247 /* Check filemode trustability */
9c28390b 248 path = git_path_buf(&buf, "config");
c869753e
SP
249 filemode = TEST_FILEMODE;
250 if (TEST_FILEMODE && !lstat(path, &st1)) {
4f629539 251 struct stat st2;
c869753e 252 filemode = (!chmod(path, st1.st_mode ^ S_IXUSR) &&
4f629539 253 !lstat(path, &st2) &&
1f32ecff
MH
254 st1.st_mode != st2.st_mode &&
255 !chmod(path, st1.st_mode));
c7bf68d6
TB
256 if (filemode && !reinit && (st1.st_mode & S_IXUSR))
257 filemode = 0;
e24317b4 258 }
3d180648 259 git_config_set("core.filemode", filemode ? "true" : "false");
ef0a89a6 260
e90fdc39 261 if (is_bare_repository())
3d180648 262 git_config_set("core.bare", "true");
7d1864ce 263 else {
e90fdc39 264 const char *work_tree = get_git_work_tree();
3d180648 265 git_config_set("core.bare", "false");
196055c2 266 /* allow template config file to override the default */
341fb286 267 if (log_all_ref_updates == LOG_REFS_UNSET)
3d180648 268 git_config_set("core.logallrefupdates", "true");
6311cfaf 269 if (needs_work_tree_config(original_git_dir, work_tree))
3d180648 270 git_config_set("core.worktree", work_tree);
7d1864ce 271 }
75d24499 272
75d24499 273 if (!reinit) {
2455406a 274 /* Check if symlink is supported in the work tree */
9c28390b 275 path = git_path_buf(&buf, "tXXXXXX");
75d24499
JH
276 if (!close(xmkstemp(path)) &&
277 !unlink(path) &&
278 !symlink("testing", path) &&
279 !lstat(path, &st1) &&
280 S_ISLNK(st1.st_mode))
281 unlink(path); /* good */
282 else
3d180648 283 git_config_set("core.symlinks", "false");
2455406a
DP
284
285 /* Check if the filesystem is case-insensitive */
9c28390b 286 path = git_path_buf(&buf, "CoNfIg");
2455406a 287 if (!access(path, F_OK))
3d180648 288 git_config_set("core.ignorecase", "true");
fdf72966 289 probe_utf8_pathname_composition();
75d24499
JH
290 }
291
9c28390b 292 strbuf_release(&buf);
ef0a89a6 293 return reinit;
cad88fdf
LT
294}
295
9173912b
JN
296static void create_object_directory(void)
297{
9c28390b
JK
298 struct strbuf path = STRBUF_INIT;
299 size_t baselen;
300
301 strbuf_addstr(&path, get_object_directory());
302 baselen = path.len;
303
304 safe_create_dir(path.buf, 1);
9173912b 305
9c28390b
JK
306 strbuf_setlen(&path, baselen);
307 strbuf_addstr(&path, "/pack");
308 safe_create_dir(path.buf, 1);
9173912b 309
9c28390b
JK
310 strbuf_setlen(&path, baselen);
311 strbuf_addstr(&path, "/info");
312 safe_create_dir(path.buf, 1);
9173912b 313
9c28390b 314 strbuf_release(&path);
9173912b
JN
315}
316
822d9406 317static void separate_git_dir(const char *git_dir, const char *git_link)
b57fb80a
NTND
318{
319 struct stat st;
b57fb80a
NTND
320
321 if (!stat(git_link, &st)) {
322 const char *src;
323
324 if (S_ISREG(st.st_mode))
13d6ec91 325 src = read_gitfile(git_link);
b57fb80a
NTND
326 else if (S_ISDIR(st.st_mode))
327 src = git_link;
328 else
97f261b1 329 die(_("unable to handle file type %d"), (int)st.st_mode);
b57fb80a
NTND
330
331 if (rename(src, git_dir))
2c050e01 332 die_errno(_("unable to move %s to %s"), src, git_dir);
b57fb80a
NTND
333 }
334
1f76a10b 335 write_file(git_link, "gitdir: %s", git_dir);
b57fb80a
NTND
336}
337
33158701
NTND
338int init_db(const char *git_dir, const char *real_git_dir,
339 const char *template_dir, unsigned int flags)
f225aeb2 340{
9173912b 341 int reinit;
1bd19079 342 int exist_ok = flags & INIT_DB_EXIST_OK;
ce83eadd 343 char *original_git_dir = real_pathdup(git_dir, 1);
33158701 344
1bd19079
NTND
345 if (real_git_dir) {
346 struct stat st;
33158701 347
1bd19079
NTND
348 if (!exist_ok && !stat(git_dir, &st))
349 die(_("%s already exists"), git_dir);
350
351 if (!exist_ok && !stat(real_git_dir, &st))
352 die(_("%s already exists"), real_git_dir);
f225aeb2 353
1bd19079 354 set_git_dir(real_path(real_git_dir));
822d9406
NTND
355 git_dir = get_git_dir();
356 separate_git_dir(git_dir, original_git_dir);
1bd19079
NTND
357 }
358 else {
359 set_git_dir(real_path(git_dir));
822d9406 360 git_dir = get_git_dir();
1bd19079
NTND
361 }
362 startup_info->have_repository = 1;
f225aeb2 363
b57fb80a 364 safe_create_dir(git_dir, 0);
f225aeb2 365
0a2c7eea
DM
366 init_is_bare_repository = is_bare_repository();
367
f225aeb2
DB
368 /* Check to see if the repository version is right.
369 * Note that a newly created repository does not have
370 * config file, so this will not fail. What we are catching
371 * is an attempt to reinitialize new repository with an old tool.
372 */
373 check_repository_format();
374
6311cfaf 375 reinit = create_default_files(template_dir, original_git_dir);
f225aeb2 376
9173912b 377 create_object_directory();
f225aeb2 378
7875acb6 379 if (get_shared_repository()) {
f225aeb2
DB
380 char buf[10];
381 /* We do not spell "group" and such, so that
382 * the configuration can be read by older version
383 * of git. Note, we use octal numbers for new share modes,
384 * and compatibility values for PERM_GROUP and
385 * PERM_EVERYBODY.
386 */
7875acb6 387 if (get_shared_repository() < 0)
5a688fe4 388 /* force to the mode value */
7875acb6
JK
389 xsnprintf(buf, sizeof(buf), "0%o", -get_shared_repository());
390 else if (get_shared_repository() == PERM_GROUP)
5096d490 391 xsnprintf(buf, sizeof(buf), "%d", OLD_PERM_GROUP);
7875acb6 392 else if (get_shared_repository() == PERM_EVERYBODY)
5096d490 393 xsnprintf(buf, sizeof(buf), "%d", OLD_PERM_EVERYBODY);
f225aeb2 394 else
033abf97 395 BUG("invalid value for shared_repository");
3d180648
PS
396 git_config_set("core.sharedrepository", buf);
397 git_config_set("receive.denyNonFastforwards", "true");
f225aeb2
DB
398 }
399
d06f15d9 400 if (!(flags & INIT_DB_QUIET)) {
d06f15d9 401 int len = strlen(git_dir);
3e5dd7e9 402
c30364d0
VA
403 if (reinit)
404 printf(get_shared_repository()
405 ? _("Reinitialized existing shared Git repository in %s%s\n")
406 : _("Reinitialized existing Git repository in %s%s\n"),
407 git_dir, len && git_dir[len-1] != '/' ? "/" : "");
408 else
409 printf(get_shared_repository()
410 ? _("Initialized empty shared Git repository in %s%s\n")
411 : _("Initialized empty Git repository in %s%s\n"),
412 git_dir, len && git_dir[len-1] != '/' ? "/" : "");
d06f15d9 413 }
f225aeb2 414
6311cfaf 415 free(original_git_dir);
f225aeb2
DB
416 return 0;
417}
418
419static int guess_repository_type(const char *git_dir)
6adcca3f 420{
6adcca3f 421 const char *slash;
56b9f6e7
RS
422 char *cwd;
423 int cwd_is_git_dir;
6adcca3f 424
6adcca3f
JH
425 /*
426 * "GIT_DIR=. git init" is always bare.
427 * "GIT_DIR=`pwd` git init" too.
428 */
429 if (!strcmp(".", git_dir))
f225aeb2 430 return 1;
56b9f6e7
RS
431 cwd = xgetcwd();
432 cwd_is_git_dir = !strcmp(git_dir, cwd);
433 free(cwd);
434 if (cwd_is_git_dir)
f225aeb2 435 return 1;
6adcca3f
JH
436 /*
437 * "GIT_DIR=.git or GIT_DIR=something/.git is usually not.
438 */
439 if (!strcmp(git_dir, ".git"))
f225aeb2 440 return 0;
6adcca3f
JH
441 slash = strrchr(git_dir, '/');
442 if (slash && !strcmp(slash, "/.git"))
f225aeb2 443 return 0;
6adcca3f
JH
444
445 /*
446 * Otherwise it is often bare. At this point
447 * we are just guessing.
448 */
f225aeb2 449 return 1;
6adcca3f
JH
450}
451
596f91ee
MK
452static int shared_callback(const struct option *opt, const char *arg, int unset)
453{
517fe807 454 BUG_ON_OPT_NEG(unset);
596f91ee
MK
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 494 if (real_git_dir && !is_absolute_path(real_git_dir))
ce83eadd 495 real_git_dir = real_pathdup(real_git_dir, 1);
b57fb80a 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 */
7875acb6
JK
508 saved = get_shared_repository();
509 set_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 }
7875acb6 521 set_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 538 if (init_shared_repository != -1)
7875acb6 539 set_shared_repository(init_shared_repository);
5a688fe4 540
6adcca3f
JH
541 /*
542 * GIT_WORK_TREE makes sense only in conjunction with GIT_DIR
543 * without --bare. Catch the error early.
544 */
e5b07c53
JK
545 git_dir = xstrdup_or_null(getenv(GIT_DIR_ENVIRONMENT));
546 work_tree = xstrdup_or_null(getenv(GIT_WORK_TREE_ENVIRONMENT));
83518360 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);
ce83eadd 566 git_work_tree_cfg = real_pathdup(rel, 1);
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
0e5bba53 584 UNLEAK(real_git_dir);
e5b07c53
JK
585 UNLEAK(git_dir);
586 UNLEAK(work_tree);
0e5bba53 587
33158701
NTND
588 flags |= INIT_DB_EXIST_OK;
589 return init_db(git_dir, real_git_dir, template_dir, flags);
e83c5163 590}