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