2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
11 #include "parse-options.h"
14 #ifdef NO_TRUSTABLE_FILEMODE
15 #define TEST_FILEMODE 0
17 #define TEST_FILEMODE 1
20 #define GIT_DEFAULT_HASH_ENVIRONMENT "GIT_DEFAULT_HASH"
22 static int init_is_bare_repository
= 0;
23 static int init_shared_repository
= -1;
25 static void copy_templates_1(struct strbuf
*path
, struct strbuf
*template_path
,
28 size_t path_baselen
= path
->len
;
29 size_t template_baselen
= template_path
->len
;
32 /* Note: if ".git/hooks" file exists in the repository being
33 * re-initialized, /etc/core-git/templates/hooks/update would
34 * cause "git init" to fail here. I think this is sane but
35 * it means that the set of templates we ship by default, along
36 * with the way the namespace under .git/ is organized, should
37 * be really carefully chosen.
39 safe_create_dir(path
->buf
, 1);
40 while ((de
= readdir(dir
)) != NULL
) {
41 struct stat st_git
, st_template
;
44 strbuf_setlen(path
, path_baselen
);
45 strbuf_setlen(template_path
, template_baselen
);
47 if (de
->d_name
[0] == '.')
49 strbuf_addstr(path
, de
->d_name
);
50 strbuf_addstr(template_path
, de
->d_name
);
51 if (lstat(path
->buf
, &st_git
)) {
53 die_errno(_("cannot stat '%s'"), path
->buf
);
58 if (lstat(template_path
->buf
, &st_template
))
59 die_errno(_("cannot stat template '%s'"), template_path
->buf
);
61 if (S_ISDIR(st_template
.st_mode
)) {
62 DIR *subdir
= opendir(template_path
->buf
);
64 die_errno(_("cannot opendir '%s'"), template_path
->buf
);
65 strbuf_addch(path
, '/');
66 strbuf_addch(template_path
, '/');
67 copy_templates_1(path
, template_path
, subdir
);
72 else if (S_ISLNK(st_template
.st_mode
)) {
73 struct strbuf lnk
= STRBUF_INIT
;
74 if (strbuf_readlink(&lnk
, template_path
->buf
,
75 st_template
.st_size
) < 0)
76 die_errno(_("cannot readlink '%s'"), template_path
->buf
);
77 if (symlink(lnk
.buf
, path
->buf
))
78 die_errno(_("cannot symlink '%s' '%s'"),
82 else if (S_ISREG(st_template
.st_mode
)) {
83 if (copy_file(path
->buf
, template_path
->buf
, st_template
.st_mode
))
84 die_errno(_("cannot copy '%s' to '%s'"),
85 template_path
->buf
, path
->buf
);
88 error(_("ignoring template %s"), template_path
->buf
);
92 static void copy_templates(const char *option_template
)
94 const char *template_dir
= get_template_dir(option_template
);
95 struct strbuf path
= STRBUF_INIT
;
96 struct strbuf template_path
= STRBUF_INIT
;
98 struct repository_format template_format
= REPOSITORY_FORMAT_INIT
;
99 struct strbuf err
= STRBUF_INIT
;
101 char *to_free
= NULL
;
103 if (!template_dir
|| !*template_dir
)
106 strbuf_addstr(&template_path
, template_dir
);
107 strbuf_complete(&template_path
, '/');
108 template_len
= template_path
.len
;
110 dir
= opendir(template_path
.buf
);
112 warning(_("templates not found in %s"), template_dir
);
116 /* Make sure that template is from the correct vintage */
117 strbuf_addstr(&template_path
, "config");
118 read_repository_format(&template_format
, template_path
.buf
);
119 strbuf_setlen(&template_path
, template_len
);
122 * No mention of version at all is OK, but anything else should be
125 if (template_format
.version
>= 0 &&
126 verify_repository_format(&template_format
, &err
) < 0) {
127 warning(_("not copying templates from '%s': %s"),
128 template_dir
, err
.buf
);
129 strbuf_release(&err
);
130 goto close_free_return
;
133 strbuf_addstr(&path
, get_git_common_dir());
134 strbuf_complete(&path
, '/');
135 copy_templates_1(&path
, &template_path
, dir
);
140 strbuf_release(&path
);
141 strbuf_release(&template_path
);
142 clear_repository_format(&template_format
);
146 * If the git_dir is not directly inside the working tree, then git will not
147 * find it by default, and we need to set the worktree explicitly.
149 static int needs_work_tree_config(const char *git_dir
, const char *work_tree
)
151 if (!strcmp(work_tree
, "/") && !strcmp(git_dir
, "/.git"))
153 if (skip_prefix(git_dir
, work_tree
, &git_dir
) &&
154 !strcmp(git_dir
, "/.git"))
159 void initialize_repository_version(int hash_algo
, int reinit
)
161 char repo_version_string
[10];
162 int repo_version
= GIT_REPO_VERSION
;
164 if (hash_algo
!= GIT_HASH_SHA1
)
165 repo_version
= GIT_REPO_VERSION_READ
;
167 /* This forces creation of new config file */
168 xsnprintf(repo_version_string
, sizeof(repo_version_string
),
170 git_config_set("core.repositoryformatversion", repo_version_string
);
172 if (hash_algo
!= GIT_HASH_SHA1
)
173 git_config_set("extensions.objectformat",
174 hash_algos
[hash_algo
].name
);
176 git_config_set_gently("extensions.objectformat", NULL
);
179 static int create_default_files(const char *template_path
,
180 const char *original_git_dir
,
181 const char *initial_branch
,
182 const struct repository_format
*fmt
,
186 struct strbuf buf
= STRBUF_INIT
;
191 struct strbuf err
= STRBUF_INIT
;
192 const char *work_tree
= get_git_work_tree();
195 * First copy the templates -- we might have the default
196 * config file there, in which case we would want to read
197 * from it after installing.
199 * Before reading that config, we also need to clear out any cached
200 * values (since we've just potentially changed what's available on
203 copy_templates(template_path
);
205 reset_shared_repository();
206 git_config(git_default_config
, NULL
);
209 * We must make sure command-line options continue to override any
210 * values we might have just re-read from the config.
212 is_bare_repository_cfg
= init_is_bare_repository
|| !work_tree
;
213 if (init_shared_repository
!= -1)
214 set_shared_repository(init_shared_repository
);
217 * We would have created the above under user's umask -- under
218 * shared-repository settings, we would need to fix them up.
220 if (get_shared_repository()) {
221 adjust_shared_perm(get_git_dir());
225 * We need to create a "refs" dir in any case so that older
226 * versions of git can tell that this is a repository.
228 safe_create_dir(git_path("refs"), 1);
229 adjust_shared_perm(git_path("refs"));
231 if (refs_init_db(&err
))
232 die("failed to set up refs db: %s", err
.buf
);
235 * Point the HEAD symref to the initial branch with if HEAD does
238 path
= git_path_buf(&buf
, "HEAD");
239 reinit
= (!access(path
, R_OK
)
240 || readlink(path
, junk
, sizeof(junk
)-1) != -1);
245 initial_branch
= git_default_branch_name(quiet
);
247 ref
= xstrfmt("refs/heads/%s", initial_branch
);
248 if (check_refname_format(ref
, 0) < 0)
249 die(_("invalid initial branch name: '%s'"),
252 if (create_symref("HEAD", ref
, NULL
) < 0)
257 initialize_repository_version(fmt
->hash_algo
, 0);
259 /* Check filemode trustability */
260 path
= git_path_buf(&buf
, "config");
261 filemode
= TEST_FILEMODE
;
262 if (TEST_FILEMODE
&& !lstat(path
, &st1
)) {
264 filemode
= (!chmod(path
, st1
.st_mode
^ S_IXUSR
) &&
265 !lstat(path
, &st2
) &&
266 st1
.st_mode
!= st2
.st_mode
&&
267 !chmod(path
, st1
.st_mode
));
268 if (filemode
&& !reinit
&& (st1
.st_mode
& S_IXUSR
))
271 git_config_set("core.filemode", filemode
? "true" : "false");
273 if (is_bare_repository())
274 git_config_set("core.bare", "true");
276 git_config_set("core.bare", "false");
277 /* allow template config file to override the default */
278 if (log_all_ref_updates
== LOG_REFS_UNSET
)
279 git_config_set("core.logallrefupdates", "true");
280 if (needs_work_tree_config(original_git_dir
, work_tree
))
281 git_config_set("core.worktree", work_tree
);
285 /* Check if symlink is supported in the work tree */
286 path
= git_path_buf(&buf
, "tXXXXXX");
287 if (!close(xmkstemp(path
)) &&
289 !symlink("testing", path
) &&
290 !lstat(path
, &st1
) &&
291 S_ISLNK(st1
.st_mode
))
292 unlink(path
); /* good */
294 git_config_set("core.symlinks", "false");
296 /* Check if the filesystem is case-insensitive */
297 path
= git_path_buf(&buf
, "CoNfIg");
298 if (!access(path
, F_OK
))
299 git_config_set("core.ignorecase", "true");
300 probe_utf8_pathname_composition();
303 strbuf_release(&buf
);
307 static void create_object_directory(void)
309 struct strbuf path
= STRBUF_INIT
;
312 strbuf_addstr(&path
, get_object_directory());
315 safe_create_dir(path
.buf
, 1);
317 strbuf_setlen(&path
, baselen
);
318 strbuf_addstr(&path
, "/pack");
319 safe_create_dir(path
.buf
, 1);
321 strbuf_setlen(&path
, baselen
);
322 strbuf_addstr(&path
, "/info");
323 safe_create_dir(path
.buf
, 1);
325 strbuf_release(&path
);
328 static void separate_git_dir(const char *git_dir
, const char *git_link
)
332 if (!stat(git_link
, &st
)) {
335 if (S_ISREG(st
.st_mode
))
336 src
= read_gitfile(git_link
);
337 else if (S_ISDIR(st
.st_mode
))
340 die(_("unable to handle file type %d"), (int)st
.st_mode
);
342 if (rename(src
, git_dir
))
343 die_errno(_("unable to move %s to %s"), src
, git_dir
);
344 repair_worktrees(NULL
, NULL
);
347 write_file(git_link
, "gitdir: %s", git_dir
);
350 static void validate_hash_algorithm(struct repository_format
*repo_fmt
, int hash
)
352 const char *env
= getenv(GIT_DEFAULT_HASH_ENVIRONMENT
);
354 * If we already have an initialized repo, don't allow the user to
355 * specify a different algorithm, as that could cause corruption.
356 * Otherwise, if the user has specified one on the command line, use it.
358 if (repo_fmt
->version
>= 0 && hash
!= GIT_HASH_UNKNOWN
&& hash
!= repo_fmt
->hash_algo
)
359 die(_("attempt to reinitialize repository with different hash"));
360 else if (hash
!= GIT_HASH_UNKNOWN
)
361 repo_fmt
->hash_algo
= hash
;
363 int env_algo
= hash_algo_by_name(env
);
364 if (env_algo
== GIT_HASH_UNKNOWN
)
365 die(_("unknown hash algorithm '%s'"), env
);
366 repo_fmt
->hash_algo
= env_algo
;
370 int init_db(const char *git_dir
, const char *real_git_dir
,
371 const char *template_dir
, int hash
, const char *initial_branch
,
375 int exist_ok
= flags
& INIT_DB_EXIST_OK
;
376 char *original_git_dir
= real_pathdup(git_dir
, 1);
377 struct repository_format repo_fmt
= REPOSITORY_FORMAT_INIT
;
382 if (!exist_ok
&& !stat(git_dir
, &st
))
383 die(_("%s already exists"), git_dir
);
385 if (!exist_ok
&& !stat(real_git_dir
, &st
))
386 die(_("%s already exists"), real_git_dir
);
388 set_git_dir(real_git_dir
, 1);
389 git_dir
= get_git_dir();
390 separate_git_dir(git_dir
, original_git_dir
);
393 set_git_dir(git_dir
, 1);
394 git_dir
= get_git_dir();
396 startup_info
->have_repository
= 1;
398 /* Ensure `core.hidedotfiles` is processed */
399 git_config(platform_core_config
, NULL
);
401 safe_create_dir(git_dir
, 0);
403 init_is_bare_repository
= is_bare_repository();
405 /* Check to see if the repository version is right.
406 * Note that a newly created repository does not have
407 * config file, so this will not fail. What we are catching
408 * is an attempt to reinitialize new repository with an old tool.
410 check_repository_format(&repo_fmt
);
412 validate_hash_algorithm(&repo_fmt
, hash
);
414 reinit
= create_default_files(template_dir
, original_git_dir
,
415 initial_branch
, &repo_fmt
,
416 flags
& INIT_DB_QUIET
);
417 if (reinit
&& initial_branch
)
418 warning(_("re-init: ignored --initial-branch=%s"),
421 create_object_directory();
423 if (get_shared_repository()) {
425 /* We do not spell "group" and such, so that
426 * the configuration can be read by older version
427 * of git. Note, we use octal numbers for new share modes,
428 * and compatibility values for PERM_GROUP and
431 if (get_shared_repository() < 0)
432 /* force to the mode value */
433 xsnprintf(buf
, sizeof(buf
), "0%o", -get_shared_repository());
434 else if (get_shared_repository() == PERM_GROUP
)
435 xsnprintf(buf
, sizeof(buf
), "%d", OLD_PERM_GROUP
);
436 else if (get_shared_repository() == PERM_EVERYBODY
)
437 xsnprintf(buf
, sizeof(buf
), "%d", OLD_PERM_EVERYBODY
);
439 BUG("invalid value for shared_repository");
440 git_config_set("core.sharedrepository", buf
);
441 git_config_set("receive.denyNonFastforwards", "true");
444 if (!(flags
& INIT_DB_QUIET
)) {
445 int len
= strlen(git_dir
);
448 printf(get_shared_repository()
449 ? _("Reinitialized existing shared Git repository in %s%s\n")
450 : _("Reinitialized existing Git repository in %s%s\n"),
451 git_dir
, len
&& git_dir
[len
-1] != '/' ? "/" : "");
453 printf(get_shared_repository()
454 ? _("Initialized empty shared Git repository in %s%s\n")
455 : _("Initialized empty Git repository in %s%s\n"),
456 git_dir
, len
&& git_dir
[len
-1] != '/' ? "/" : "");
459 free(original_git_dir
);
463 static int guess_repository_type(const char *git_dir
)
470 * "GIT_DIR=. git init" is always bare.
471 * "GIT_DIR=`pwd` git init" too.
473 if (!strcmp(".", git_dir
))
476 cwd_is_git_dir
= !strcmp(git_dir
, cwd
);
481 * "GIT_DIR=.git or GIT_DIR=something/.git is usually not.
483 if (!strcmp(git_dir
, ".git"))
485 slash
= strrchr(git_dir
, '/');
486 if (slash
&& !strcmp(slash
, "/.git"))
490 * Otherwise it is often bare. At this point
491 * we are just guessing.
496 static int shared_callback(const struct option
*opt
, const char *arg
, int unset
)
498 BUG_ON_OPT_NEG(unset
);
499 *((int *) opt
->value
) = (arg
) ? git_config_perm("arg", arg
) : PERM_GROUP
;
503 static const char *const init_db_usage
[] = {
504 N_("git init [-q | --quiet] [--bare] [--template=<template-directory>]\n"
505 " [--separate-git-dir <git-dir>] [--object-format=<format>]\n"
506 " [-b <branch-name> | --initial-branch=<branch-name>]\n"
507 " [--shared[=<permissions>]] [<directory>]"),
512 * If you want to, you can share the DB area with any number of branches.
513 * That has advantages: you can save space by sharing all the SHA1 objects.
514 * On the other hand, it might just make lookup slower and messier. You
515 * be the judge. The default case is to have one DB per managed directory.
517 int cmd_init_db(int argc
, const char **argv
, const char *prefix
)
520 const char *real_git_dir
= NULL
;
521 const char *work_tree
;
522 const char *template_dir
= NULL
;
523 unsigned int flags
= 0;
524 const char *object_format
= NULL
;
525 const char *initial_branch
= NULL
;
526 int hash_algo
= GIT_HASH_UNKNOWN
;
527 const struct option init_db_options
[] = {
528 OPT_STRING(0, "template", &template_dir
, N_("template-directory"),
529 N_("directory from which templates will be used")),
530 OPT_SET_INT(0, "bare", &is_bare_repository_cfg
,
531 N_("create a bare repository"), 1),
532 { OPTION_CALLBACK
, 0, "shared", &init_shared_repository
,
534 N_("specify that the git repository is to be shared amongst several users"),
535 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
, shared_callback
, 0},
536 OPT_BIT('q', "quiet", &flags
, N_("be quiet"), INIT_DB_QUIET
),
537 OPT_STRING(0, "separate-git-dir", &real_git_dir
, N_("gitdir"),
538 N_("separate git dir from working tree")),
539 OPT_STRING('b', "initial-branch", &initial_branch
, N_("name"),
540 N_("override the name of the initial branch")),
541 OPT_STRING(0, "object-format", &object_format
, N_("hash"),
542 N_("specify the hash algorithm to use")),
546 argc
= parse_options(argc
, argv
, prefix
, init_db_options
, init_db_usage
, 0);
548 if (real_git_dir
&& is_bare_repository_cfg
== 1)
549 die(_("options '%s' and '%s' cannot be used together"), "--separate-git-dir", "--bare");
551 if (real_git_dir
&& !is_absolute_path(real_git_dir
))
552 real_git_dir
= real_pathdup(real_git_dir
, 1);
554 if (template_dir
&& *template_dir
&& !is_absolute_path(template_dir
)) {
555 template_dir
= absolute_pathdup(template_dir
);
556 UNLEAK(template_dir
);
562 if (chdir(argv
[0]) < 0) {
566 * At this point we haven't read any configuration,
567 * and we know shared_repository should always be 0;
568 * but just in case we play safe.
570 saved
= get_shared_repository();
571 set_shared_repository(0);
572 switch (safe_create_leading_directories_const(argv
[0])) {
580 die_errno(_("cannot mkdir %s"), argv
[0]);
583 set_shared_repository(saved
);
584 if (mkdir(argv
[0], 0777) < 0)
585 die_errno(_("cannot mkdir %s"), argv
[0]);
589 die_errno(_("cannot chdir to %s"), argv
[0]);
591 } else if (0 < argc
) {
592 usage(init_db_usage
[0]);
863 if (is_bare_repository_cfg
== 1) {
595 char *cwd
= xgetcwd();
596 setenv(GIT_DIR_ENVIRONMENT
, cwd
, argc
> 0);
601 hash_algo
= hash_algo_by_name(object_format
);
602 if (hash_algo
== GIT_HASH_UNKNOWN
)
603 die(_("unknown hash algorithm '%s'"), object_format
);
606 if (init_shared_repository
!= -1)
607 set_shared_repository(init_shared_repository
);
610 * GIT_WORK_TREE makes sense only in conjunction with GIT_DIR
611 * without --bare. Catch the error early.
613 git_dir
= xstrdup_or_null(getenv(GIT_DIR_ENVIRONMENT
));
614 work_tree
= xstrdup_or_null(getenv(GIT_WORK_TREE_ENVIRONMENT
));
615 if ((!git_dir
|| is_bare_repository_cfg
== 1) && work_tree
)
616 die(_("%s (or --work-tree=<directory>) not allowed without "
617 "specifying %s (or --git-dir=<directory>)"),
618 GIT_WORK_TREE_ENVIRONMENT
,
619 GIT_DIR_ENVIRONMENT
);
622 * Set up the default .git directory contents
625 git_dir
= DEFAULT_GIT_DIR_ENVIRONMENT
;
628 * When --separate-git-dir is used inside a linked worktree, take
629 * care to ensure that the common .git/ directory is relocated, not
630 * the worktree-specific .git/worktrees/<id>/ directory.
635 struct strbuf sb
= STRBUF_INIT
;
637 p
= read_gitfile_gently(git_dir
, &err
);
638 if (p
&& get_common_dir(&sb
, p
)) {
639 struct strbuf mainwt
= STRBUF_INIT
;
641 strbuf_addbuf(&mainwt
, &sb
);
642 strbuf_strip_suffix(&mainwt
, "/.git");
643 if (chdir(mainwt
.buf
) < 0)
644 die_errno(_("cannot chdir to %s"), mainwt
.buf
);
645 strbuf_release(&mainwt
);
646 git_dir
= strbuf_detach(&sb
, NULL
);
651 if (is_bare_repository_cfg
< 0)
652 is_bare_repository_cfg
= guess_repository_type(git_dir
);
654 if (!is_bare_repository_cfg
) {
655 const char *git_dir_parent
= strrchr(git_dir
, '/');
656 if (git_dir_parent
) {
657 char *rel
= xstrndup(git_dir
, git_dir_parent
- git_dir
);
658 git_work_tree_cfg
= real_pathdup(rel
, 1);
661 if (!git_work_tree_cfg
)
662 git_work_tree_cfg
= xgetcwd();
664 set_git_work_tree(work_tree
);
666 set_git_work_tree(git_work_tree_cfg
);
667 if (access(get_git_work_tree(), X_OK
))
668 die_errno (_("Cannot access work tree '%s'"),
669 get_git_work_tree());
673 die(_("--separate-git-dir incompatible with bare repository"));
675 set_git_work_tree(work_tree
);
678 UNLEAK(real_git_dir
);
682 flags
|= INIT_DB_EXIST_OK
;
683 return init_db(git_dir
, real_git_dir
, template_dir
, hash_algo
,
684 initial_branch
, flags
);