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