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