]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/init-db.c
builtin/init-db: allow specifying hash algorithm on command line
[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;
e8805af1 99 struct repository_format template_format = REPOSITORY_FORMAT_INIT;
94ce1672 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);
e8805af1 151 clear_repository_format(&template_format);
8d5afef0
JH
152}
153
90b45187
SD
154static int git_init_db_config(const char *k, const char *v, void *cb)
155{
90b45187
SD
156 if (!strcmp(k, "init.templatedir"))
157 return git_config_pathname(&init_db_template_dir, k, v);
158
28785339
JS
159 if (starts_with(k, "core."))
160 return platform_core_config(k, v, cb);
161
90b45187
SD
162 return 0;
163}
164
84ccad8d
JK
165/*
166 * If the git_dir is not directly inside the working tree, then git will not
167 * find it by default, and we need to set the worktree explicitly.
168 */
169static int needs_work_tree_config(const char *git_dir, const char *work_tree)
170{
171 if (!strcmp(work_tree, "/") && !strcmp(git_dir, "/.git"))
172 return 0;
173 if (skip_prefix(git_dir, work_tree, &git_dir) &&
174 !strcmp(git_dir, "/.git"))
175 return 0;
176 return 1;
177}
178
6311cfaf 179static int create_default_files(const char *template_path,
8b8f7189 180 const char *original_git_dir,
181 const struct repository_format *fmt)
cad88fdf 182{
4f629539 183 struct stat st1;
9c28390b
JK
184 struct strbuf buf = STRBUF_INIT;
185 char *path;
4f629539 186 char repo_version_string[10];
5cc8f372 187 char junk[2];
ef0a89a6 188 int reinit;
c869753e 189 int filemode;
6fb5acfd 190 struct strbuf err = STRBUF_INIT;
8b8f7189 191 int repo_version = GIT_REPO_VERSION;
cad88fdf 192
90b45187 193 /* Just look for `init.templatedir` */
28785339 194 init_db_template_dir = NULL; /* re-set in case it was set before */
90b45187
SD
195 git_config(git_init_db_config, NULL);
196
7c0a842b
JK
197 /*
198 * First copy the templates -- we might have the default
4f629539
JH
199 * config file there, in which case we would want to read
200 * from it after installing.
4543926b
JK
201 *
202 * Before reading that config, we also need to clear out any cached
203 * values (since we've just potentially changed what's available on
204 * disk).
4f629539 205 */
f225aeb2 206 copy_templates(template_path);
4543926b
JK
207 git_config_clear();
208 reset_shared_repository();
ef90d6d4 209 git_config(git_default_config, NULL);
5a688fe4 210
7c0a842b
JK
211 /*
212 * We must make sure command-line options continue to override any
213 * values we might have just re-read from the config.
214 */
215 is_bare_repository_cfg = init_is_bare_repository;
0a2c7eea 216 if (init_shared_repository != -1)
7875acb6 217 set_shared_repository(init_shared_repository);
4f629539 218
138086a7
JH
219 /*
220 * We would have created the above under user's umask -- under
221 * shared-repository settings, we would need to fix them up.
222 */
7875acb6 223 if (get_shared_repository()) {
f225aeb2 224 adjust_shared_perm(get_git_dir());
138086a7
JH
225 }
226
6fb5acfd
DT
227 /*
228 * We need to create a "refs" dir in any case so that older
229 * versions of git can tell that this is a repository.
230 */
231 safe_create_dir(git_path("refs"), 1);
232 adjust_shared_perm(git_path("refs"));
233
234 if (refs_init_db(&err))
235 die("failed to set up refs db: %s", err.buf);
236
cad88fdf
LT
237 /*
238 * Create the default symlink from ".git/HEAD" to the "master"
8098a178 239 * branch, if it does not exist yet.
cad88fdf 240 */
9c28390b 241 path = git_path_buf(&buf, "HEAD");
5cc8f372
JS
242 reinit = (!access(path, R_OK)
243 || readlink(path, junk, sizeof(junk)-1) != -1);
ef0a89a6 244 if (!reinit) {
8b5157e4 245 if (create_symref("HEAD", "refs/heads/master", NULL) < 0)
cad88fdf 246 exit(1);
cad88fdf 247 }
e24317b4 248
8b8f7189 249#ifndef ENABLE_SHA256
250 if (fmt->hash_algo != GIT_HASH_SHA1)
251 die(_("The hash algorithm %s is not supported in this build."), hash_algos[fmt->hash_algo].name);
252#endif
253
254 if (fmt->hash_algo != GIT_HASH_SHA1)
255 repo_version = GIT_REPO_VERSION_READ;
256
4f629539 257 /* This forces creation of new config file */
5096d490 258 xsnprintf(repo_version_string, sizeof(repo_version_string),
8b8f7189 259 "%d", repo_version);
3d180648 260 git_config_set("core.repositoryformatversion", repo_version_string);
4f629539 261
8b8f7189 262 if (fmt->hash_algo != GIT_HASH_SHA1)
263 git_config_set("extensions.objectformat",
264 hash_algos[fmt->hash_algo].name);
265
4f629539 266 /* Check filemode trustability */
9c28390b 267 path = git_path_buf(&buf, "config");
c869753e
SP
268 filemode = TEST_FILEMODE;
269 if (TEST_FILEMODE && !lstat(path, &st1)) {
4f629539 270 struct stat st2;
c869753e 271 filemode = (!chmod(path, st1.st_mode ^ S_IXUSR) &&
4f629539 272 !lstat(path, &st2) &&
1f32ecff
MH
273 st1.st_mode != st2.st_mode &&
274 !chmod(path, st1.st_mode));
c7bf68d6
TB
275 if (filemode && !reinit && (st1.st_mode & S_IXUSR))
276 filemode = 0;
e24317b4 277 }
3d180648 278 git_config_set("core.filemode", filemode ? "true" : "false");
ef0a89a6 279
e90fdc39 280 if (is_bare_repository())
3d180648 281 git_config_set("core.bare", "true");
7d1864ce 282 else {
e90fdc39 283 const char *work_tree = get_git_work_tree();
3d180648 284 git_config_set("core.bare", "false");
196055c2 285 /* allow template config file to override the default */
341fb286 286 if (log_all_ref_updates == LOG_REFS_UNSET)
3d180648 287 git_config_set("core.logallrefupdates", "true");
6311cfaf 288 if (needs_work_tree_config(original_git_dir, work_tree))
3d180648 289 git_config_set("core.worktree", work_tree);
7d1864ce 290 }
75d24499 291
75d24499 292 if (!reinit) {
2455406a 293 /* Check if symlink is supported in the work tree */
9c28390b 294 path = git_path_buf(&buf, "tXXXXXX");
75d24499
JH
295 if (!close(xmkstemp(path)) &&
296 !unlink(path) &&
297 !symlink("testing", path) &&
298 !lstat(path, &st1) &&
299 S_ISLNK(st1.st_mode))
300 unlink(path); /* good */
301 else
3d180648 302 git_config_set("core.symlinks", "false");
2455406a
DP
303
304 /* Check if the filesystem is case-insensitive */
9c28390b 305 path = git_path_buf(&buf, "CoNfIg");
2455406a 306 if (!access(path, F_OK))
3d180648 307 git_config_set("core.ignorecase", "true");
fdf72966 308 probe_utf8_pathname_composition();
75d24499
JH
309 }
310
9c28390b 311 strbuf_release(&buf);
ef0a89a6 312 return reinit;
cad88fdf
LT
313}
314
9173912b
JN
315static void create_object_directory(void)
316{
9c28390b
JK
317 struct strbuf path = STRBUF_INIT;
318 size_t baselen;
319
320 strbuf_addstr(&path, get_object_directory());
321 baselen = path.len;
322
323 safe_create_dir(path.buf, 1);
9173912b 324
9c28390b
JK
325 strbuf_setlen(&path, baselen);
326 strbuf_addstr(&path, "/pack");
327 safe_create_dir(path.buf, 1);
9173912b 328
9c28390b
JK
329 strbuf_setlen(&path, baselen);
330 strbuf_addstr(&path, "/info");
331 safe_create_dir(path.buf, 1);
9173912b 332
9c28390b 333 strbuf_release(&path);
9173912b
JN
334}
335
822d9406 336static void separate_git_dir(const char *git_dir, const char *git_link)
b57fb80a
NTND
337{
338 struct stat st;
b57fb80a
NTND
339
340 if (!stat(git_link, &st)) {
341 const char *src;
342
343 if (S_ISREG(st.st_mode))
13d6ec91 344 src = read_gitfile(git_link);
b57fb80a
NTND
345 else if (S_ISDIR(st.st_mode))
346 src = git_link;
347 else
97f261b1 348 die(_("unable to handle file type %d"), (int)st.st_mode);
b57fb80a
NTND
349
350 if (rename(src, git_dir))
2c050e01 351 die_errno(_("unable to move %s to %s"), src, git_dir);
b57fb80a
NTND
352 }
353
1f76a10b 354 write_file(git_link, "gitdir: %s", git_dir);
b57fb80a
NTND
355}
356
8b8f7189 357static void validate_hash_algorithm(struct repository_format *repo_fmt, int hash)
358{
359 /*
360 * If we already have an initialized repo, don't allow the user to
361 * specify a different algorithm, as that could cause corruption.
362 * Otherwise, if the user has specified one on the command line, use it.
363 */
364 if (repo_fmt->version >= 0 && hash != GIT_HASH_UNKNOWN && hash != repo_fmt->hash_algo)
365 die(_("attempt to reinitialize repository with different hash"));
366 else if (hash != GIT_HASH_UNKNOWN)
367 repo_fmt->hash_algo = hash;
368}
369
33158701 370int init_db(const char *git_dir, const char *real_git_dir,
8b8f7189 371 const char *template_dir, int hash, unsigned int flags)
f225aeb2 372{
9173912b 373 int reinit;
1bd19079 374 int exist_ok = flags & INIT_DB_EXIST_OK;
ce83eadd 375 char *original_git_dir = real_pathdup(git_dir, 1);
8b8f7189 376 struct repository_format repo_fmt = REPOSITORY_FORMAT_INIT;
33158701 377
1bd19079
NTND
378 if (real_git_dir) {
379 struct stat st;
33158701 380
1bd19079
NTND
381 if (!exist_ok && !stat(git_dir, &st))
382 die(_("%s already exists"), git_dir);
383
384 if (!exist_ok && !stat(real_git_dir, &st))
385 die(_("%s already exists"), real_git_dir);
f225aeb2 386
1bd19079 387 set_git_dir(real_path(real_git_dir));
822d9406
NTND
388 git_dir = get_git_dir();
389 separate_git_dir(git_dir, original_git_dir);
1bd19079
NTND
390 }
391 else {
392 set_git_dir(real_path(git_dir));
822d9406 393 git_dir = get_git_dir();
1bd19079
NTND
394 }
395 startup_info->have_repository = 1;
f225aeb2 396
28785339
JS
397 /* Just look for `core.hidedotfiles` */
398 git_config(git_init_db_config, NULL);
399
b57fb80a 400 safe_create_dir(git_dir, 0);
f225aeb2 401
0a2c7eea
DM
402 init_is_bare_repository = is_bare_repository();
403
f225aeb2
DB
404 /* Check to see if the repository version is right.
405 * Note that a newly created repository does not have
406 * config file, so this will not fail. What we are catching
407 * is an attempt to reinitialize new repository with an old tool.
408 */
8b8f7189 409 check_repository_format(&repo_fmt);
f225aeb2 410
8b8f7189 411 validate_hash_algorithm(&repo_fmt, hash);
412
413 reinit = create_default_files(template_dir, original_git_dir, &repo_fmt);
f225aeb2 414
9173912b 415 create_object_directory();
f225aeb2 416
7875acb6 417 if (get_shared_repository()) {
f225aeb2
DB
418 char buf[10];
419 /* We do not spell "group" and such, so that
420 * the configuration can be read by older version
421 * of git. Note, we use octal numbers for new share modes,
422 * and compatibility values for PERM_GROUP and
423 * PERM_EVERYBODY.
424 */
7875acb6 425 if (get_shared_repository() < 0)
5a688fe4 426 /* force to the mode value */
7875acb6
JK
427 xsnprintf(buf, sizeof(buf), "0%o", -get_shared_repository());
428 else if (get_shared_repository() == PERM_GROUP)
5096d490 429 xsnprintf(buf, sizeof(buf), "%d", OLD_PERM_GROUP);
7875acb6 430 else if (get_shared_repository() == PERM_EVERYBODY)
5096d490 431 xsnprintf(buf, sizeof(buf), "%d", OLD_PERM_EVERYBODY);
f225aeb2 432 else
033abf97 433 BUG("invalid value for shared_repository");
3d180648
PS
434 git_config_set("core.sharedrepository", buf);
435 git_config_set("receive.denyNonFastforwards", "true");
f225aeb2
DB
436 }
437
d06f15d9 438 if (!(flags & INIT_DB_QUIET)) {
d06f15d9 439 int len = strlen(git_dir);
3e5dd7e9 440
c30364d0
VA
441 if (reinit)
442 printf(get_shared_repository()
443 ? _("Reinitialized existing shared Git repository in %s%s\n")
444 : _("Reinitialized existing Git repository in %s%s\n"),
445 git_dir, len && git_dir[len-1] != '/' ? "/" : "");
446 else
447 printf(get_shared_repository()
448 ? _("Initialized empty shared Git repository in %s%s\n")
449 : _("Initialized empty Git repository in %s%s\n"),
450 git_dir, len && git_dir[len-1] != '/' ? "/" : "");
d06f15d9 451 }
f225aeb2 452
6311cfaf 453 free(original_git_dir);
f225aeb2
DB
454 return 0;
455}
456
457static int guess_repository_type(const char *git_dir)
6adcca3f 458{
6adcca3f 459 const char *slash;
56b9f6e7
RS
460 char *cwd;
461 int cwd_is_git_dir;
6adcca3f 462
6adcca3f
JH
463 /*
464 * "GIT_DIR=. git init" is always bare.
465 * "GIT_DIR=`pwd` git init" too.
466 */
467 if (!strcmp(".", git_dir))
f225aeb2 468 return 1;
56b9f6e7
RS
469 cwd = xgetcwd();
470 cwd_is_git_dir = !strcmp(git_dir, cwd);
471 free(cwd);
472 if (cwd_is_git_dir)
f225aeb2 473 return 1;
6adcca3f
JH
474 /*
475 * "GIT_DIR=.git or GIT_DIR=something/.git is usually not.
476 */
477 if (!strcmp(git_dir, ".git"))
f225aeb2 478 return 0;
6adcca3f
JH
479 slash = strrchr(git_dir, '/');
480 if (slash && !strcmp(slash, "/.git"))
f225aeb2 481 return 0;
6adcca3f
JH
482
483 /*
484 * Otherwise it is often bare. At this point
485 * we are just guessing.
486 */
f225aeb2 487 return 1;
6adcca3f
JH
488}
489
596f91ee
MK
490static int shared_callback(const struct option *opt, const char *arg, int unset)
491{
517fe807 492 BUG_ON_OPT_NEG(unset);
596f91ee
MK
493 *((int *) opt->value) = (arg) ? git_config_perm("arg", arg) : PERM_GROUP;
494 return 0;
495}
496
497static const char *const init_db_usage[] = {
9c9b4f2f 498 N_("git init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]] [<directory>]"),
596f91ee
MK
499 NULL
500};
d3af621b 501
4696cb93
ZW
502/*
503 * If you want to, you can share the DB area with any number of branches.
504 * That has advantages: you can save space by sharing all the SHA1 objects.
505 * On the other hand, it might just make lookup slower and messier. You
506 * be the judge. The default case is to have one DB per managed directory.
507 */
a633fca0 508int cmd_init_db(int argc, const char **argv, const char *prefix)
e83c5163 509{
cad88fdf 510 const char *git_dir;
b57fb80a 511 const char *real_git_dir = NULL;
83518360 512 const char *work_tree;
c3c8835f 513 const char *template_dir = NULL;
f225aeb2 514 unsigned int flags = 0;
8b8f7189 515 const char *object_format = NULL;
516 int hash_algo = GIT_HASH_UNKNOWN;
596f91ee 517 const struct option init_db_options[] = {
ce4a5e53
NTND
518 OPT_STRING(0, "template", &template_dir, N_("template-directory"),
519 N_("directory from which templates will be used")),
596f91ee 520 OPT_SET_INT(0, "bare", &is_bare_repository_cfg,
ce4a5e53 521 N_("create a bare repository"), 1),
596f91ee 522 { OPTION_CALLBACK, 0, "shared", &init_shared_repository,
ce4a5e53
NTND
523 N_("permissions"),
524 N_("specify that the git repository is to be shared amongst several users"),
596f91ee 525 PARSE_OPT_OPTARG | PARSE_OPT_NONEG, shared_callback, 0},
ce4a5e53
NTND
526 OPT_BIT('q', "quiet", &flags, N_("be quiet"), INIT_DB_QUIET),
527 OPT_STRING(0, "separate-git-dir", &real_git_dir, N_("gitdir"),
528 N_("separate git dir from working tree")),
8b8f7189 529 OPT_STRING(0, "object-format", &object_format, N_("hash"),
530 N_("specify the hash algorithm to use")),
596f91ee
MK
531 OPT_END()
532 };
533
0397ff24
JH
534 argc = parse_options(argc, argv, prefix, init_db_options, init_db_usage, 0);
535
b57fb80a 536 if (real_git_dir && !is_absolute_path(real_git_dir))
ce83eadd 537 real_git_dir = real_pathdup(real_git_dir, 1);
b57fb80a 538
e1df7fe4
NTND
539 if (template_dir && *template_dir && !is_absolute_path(template_dir))
540 template_dir = absolute_pathdup(template_dir);
541
0397ff24 542 if (argc == 1) {
53d48885
NS
543 int mkdir_tried = 0;
544 retry:
0397ff24 545 if (chdir(argv[0]) < 0) {
53d48885
NS
546 if (!mkdir_tried) {
547 int saved;
548 /*
549 * At this point we haven't read any configuration,
550 * and we know shared_repository should always be 0;
551 * but just in case we play safe.
552 */
7875acb6
JK
553 saved = get_shared_repository();
554 set_shared_repository(0);
0397ff24 555 switch (safe_create_leading_directories_const(argv[0])) {
f3565c0c
MH
556 case SCLD_OK:
557 case SCLD_PERMS:
558 break;
0be0521b 559 case SCLD_EXISTS:
53d48885
NS
560 errno = EEXIST;
561 /* fallthru */
53d48885 562 default:
f3565c0c 563 die_errno(_("cannot mkdir %s"), argv[0]);
53d48885
NS
564 break;
565 }
7875acb6 566 set_shared_repository(saved);
0397ff24 567 if (mkdir(argv[0], 0777) < 0)
33e92e47 568 die_errno(_("cannot mkdir %s"), argv[0]);
53d48885
NS
569 mkdir_tried = 1;
570 goto retry;
571 }
33e92e47 572 die_errno(_("cannot chdir to %s"), argv[0]);
53d48885 573 }
0397ff24
JH
574 } else if (0 < argc) {
575 usage(init_db_usage[0]);
d3af621b 576 }
0397ff24 577 if (is_bare_repository_cfg == 1) {
4d3ab44d
RS
578 char *cwd = xgetcwd();
579 setenv(GIT_DIR_ENVIRONMENT, cwd, argc > 0);
580 free(cwd);
d3af621b
JH
581 }
582
8b8f7189 583 if (object_format) {
584 hash_algo = hash_algo_by_name(object_format);
585 if (hash_algo == GIT_HASH_UNKNOWN)
586 die(_("unknown hash algorithm '%s'"), object_format);
587 }
588
5a688fe4 589 if (init_shared_repository != -1)
7875acb6 590 set_shared_repository(init_shared_repository);
5a688fe4 591
6adcca3f
JH
592 /*
593 * GIT_WORK_TREE makes sense only in conjunction with GIT_DIR
594 * without --bare. Catch the error early.
595 */
e5b07c53
JK
596 git_dir = xstrdup_or_null(getenv(GIT_DIR_ENVIRONMENT));
597 work_tree = xstrdup_or_null(getenv(GIT_WORK_TREE_ENVIRONMENT));
83518360 598 if ((!git_dir || is_bare_repository_cfg == 1) && work_tree)
33e92e47
ÆAB
599 die(_("%s (or --work-tree=<directory>) not allowed without "
600 "specifying %s (or --git-dir=<directory>)"),
6adcca3f
JH
601 GIT_WORK_TREE_ENVIRONMENT,
602 GIT_DIR_ENVIRONMENT);
603
cad88fdf
LT
604 /*
605 * Set up the default .git directory contents
606 */
ef0a89a6 607 if (!git_dir)
cad88fdf 608 git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
af6e277c 609
f225aeb2
DB
610 if (is_bare_repository_cfg < 0)
611 is_bare_repository_cfg = guess_repository_type(git_dir);
612
613 if (!is_bare_repository_cfg) {
b31d2022
NTND
614 const char *git_dir_parent = strrchr(git_dir, '/');
615 if (git_dir_parent) {
616 char *rel = xstrndup(git_dir, git_dir_parent - git_dir);
ce83eadd 617 git_work_tree_cfg = real_pathdup(rel, 1);
b31d2022 618 free(rel);
f225aeb2 619 }
56b9f6e7
RS
620 if (!git_work_tree_cfg)
621 git_work_tree_cfg = xgetcwd();
83518360 622 if (work_tree)
2d186c8b 623 set_git_work_tree(work_tree);
83518360
NTND
624 else
625 set_git_work_tree(git_work_tree_cfg);
f225aeb2 626 if (access(get_git_work_tree(), X_OK))
33e92e47 627 die_errno (_("Cannot access work tree '%s'"),
0721c314 628 get_git_work_tree());
94df2506 629 }
83518360
NTND
630 else {
631 if (work_tree)
2d186c8b 632 set_git_work_tree(work_tree);
83518360 633 }
af6e277c 634
0e5bba53 635 UNLEAK(real_git_dir);
e5b07c53
JK
636 UNLEAK(git_dir);
637 UNLEAK(work_tree);
0e5bba53 638
33158701 639 flags |= INIT_DB_EXIST_OK;
8b8f7189 640 return init_db(git_dir, real_git_dir, template_dir, hash_algo, flags);
e83c5163 641}