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