2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
9 #include "environment.h"
14 #include "object-file.h"
15 #include "parse-options.h"
20 #ifndef DEFAULT_GIT_TEMPLATE_DIR
21 #define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates"
24 #ifdef NO_TRUSTABLE_FILEMODE
25 #define TEST_FILEMODE 0
27 #define TEST_FILEMODE 1
30 #define GIT_DEFAULT_HASH_ENVIRONMENT "GIT_DEFAULT_HASH"
32 static int init_is_bare_repository
= 0;
33 static int init_shared_repository
= -1;
35 static void copy_templates_1(struct strbuf
*path
, struct strbuf
*template_path
,
38 size_t path_baselen
= path
->len
;
39 size_t template_baselen
= template_path
->len
;
42 /* Note: if ".git/hooks" file exists in the repository being
43 * re-initialized, /etc/core-git/templates/hooks/update would
44 * cause "git init" to fail here. I think this is sane but
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.
49 safe_create_dir(path
->buf
, 1);
50 while ((de
= readdir(dir
)) != NULL
) {
51 struct stat st_git
, st_template
;
54 strbuf_setlen(path
, path_baselen
);
55 strbuf_setlen(template_path
, template_baselen
);
57 if (de
->d_name
[0] == '.')
59 strbuf_addstr(path
, de
->d_name
);
60 strbuf_addstr(template_path
, de
->d_name
);
61 if (lstat(path
->buf
, &st_git
)) {
63 die_errno(_("cannot stat '%s'"), path
->buf
);
68 if (lstat(template_path
->buf
, &st_template
))
69 die_errno(_("cannot stat template '%s'"), template_path
->buf
);
71 if (S_ISDIR(st_template
.st_mode
)) {
72 DIR *subdir
= opendir(template_path
->buf
);
74 die_errno(_("cannot opendir '%s'"), template_path
->buf
);
75 strbuf_addch(path
, '/');
76 strbuf_addch(template_path
, '/');
77 copy_templates_1(path
, template_path
, subdir
);
82 else if (S_ISLNK(st_template
.st_mode
)) {
83 struct strbuf lnk
= STRBUF_INIT
;
84 if (strbuf_readlink(&lnk
, template_path
->buf
,
85 st_template
.st_size
) < 0)
86 die_errno(_("cannot readlink '%s'"), template_path
->buf
);
87 if (symlink(lnk
.buf
, path
->buf
))
88 die_errno(_("cannot symlink '%s' '%s'"),
92 else if (S_ISREG(st_template
.st_mode
)) {
93 if (copy_file(path
->buf
, template_path
->buf
, st_template
.st_mode
))
94 die_errno(_("cannot copy '%s' to '%s'"),
95 template_path
->buf
, path
->buf
);
98 error(_("ignoring template %s"), template_path
->buf
);
102 static void copy_templates(const char *template_dir
, const char *init_template_dir
)
104 struct strbuf path
= STRBUF_INIT
;
105 struct strbuf template_path
= STRBUF_INIT
;
107 struct repository_format template_format
= REPOSITORY_FORMAT_INIT
;
108 struct strbuf err
= STRBUF_INIT
;
110 char *to_free
= NULL
;
113 template_dir
= getenv(TEMPLATE_DIR_ENVIRONMENT
);
115 template_dir
= init_template_dir
;
117 template_dir
= to_free
= system_path(DEFAULT_GIT_TEMPLATE_DIR
);
118 if (!template_dir
[0]) {
123 strbuf_addstr(&template_path
, template_dir
);
124 strbuf_complete(&template_path
, '/');
125 template_len
= template_path
.len
;
127 dir
= opendir(template_path
.buf
);
129 warning(_("templates not found in %s"), template_dir
);
133 /* Make sure that template is from the correct vintage */
134 strbuf_addstr(&template_path
, "config");
135 read_repository_format(&template_format
, template_path
.buf
);
136 strbuf_setlen(&template_path
, template_len
);
139 * No mention of version at all is OK, but anything else should be
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
);
147 goto close_free_return
;
150 strbuf_addstr(&path
, get_git_common_dir());
151 strbuf_complete(&path
, '/');
152 copy_templates_1(&path
, &template_path
, dir
);
157 strbuf_release(&path
);
158 strbuf_release(&template_path
);
159 clear_repository_format(&template_format
);
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.
166 static int needs_work_tree_config(const char *git_dir
, const char *work_tree
)
168 if (!strcmp(work_tree
, "/") && !strcmp(git_dir
, "/.git"))
170 if (skip_prefix(git_dir
, work_tree
, &git_dir
) &&
171 !strcmp(git_dir
, "/.git"))
176 void initialize_repository_version(int hash_algo
, int reinit
)
178 char repo_version_string
[10];
179 int repo_version
= GIT_REPO_VERSION
;
181 if (hash_algo
!= GIT_HASH_SHA1
)
182 repo_version
= GIT_REPO_VERSION_READ
;
184 /* This forces creation of new config file */
185 xsnprintf(repo_version_string
, sizeof(repo_version_string
),
187 git_config_set("core.repositoryformatversion", repo_version_string
);
189 if (hash_algo
!= GIT_HASH_SHA1
)
190 git_config_set("extensions.objectformat",
191 hash_algos
[hash_algo
].name
);
193 git_config_set_gently("extensions.objectformat", NULL
);
196 static int create_default_files(const char *template_path
,
197 const char *original_git_dir
,
198 const char *initial_branch
,
199 const struct repository_format
*fmt
,
203 struct strbuf buf
= STRBUF_INIT
;
208 struct strbuf err
= STRBUF_INIT
;
209 const char *init_template_dir
= NULL
;
210 const char *work_tree
= get_git_work_tree();
213 * First copy the templates -- we might have the default
214 * config file there, in which case we would want to read
215 * from it after installing.
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
221 git_config_get_pathname("init.templatedir", &init_template_dir
);
222 copy_templates(template_path
, init_template_dir
);
223 free((char *)init_template_dir
);
225 reset_shared_repository();
226 git_config(git_default_config
, NULL
);
229 * We must make sure command-line options continue to override any
230 * values we might have just re-read from the config.
232 is_bare_repository_cfg
= init_is_bare_repository
|| !work_tree
;
233 if (init_shared_repository
!= -1)
234 set_shared_repository(init_shared_repository
);
237 * We would have created the above under user's umask -- under
238 * shared-repository settings, we would need to fix them up.
240 if (get_shared_repository()) {
241 adjust_shared_perm(get_git_dir());
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.
248 safe_create_dir(git_path("refs"), 1);
249 adjust_shared_perm(git_path("refs"));
251 if (refs_init_db(&err
))
252 die("failed to set up refs db: %s", err
.buf
);
255 * Point the HEAD symref to the initial branch with if HEAD does
258 path
= git_path_buf(&buf
, "HEAD");
259 reinit
= (!access(path
, R_OK
)
260 || readlink(path
, junk
, sizeof(junk
)-1) != -1);
265 initial_branch
= git_default_branch_name(quiet
);
267 ref
= xstrfmt("refs/heads/%s", initial_branch
);
268 if (check_refname_format(ref
, 0) < 0)
269 die(_("invalid initial branch name: '%s'"),
272 if (create_symref("HEAD", ref
, NULL
) < 0)
277 initialize_repository_version(fmt
->hash_algo
, 0);
279 /* Check filemode trustability */
280 path
= git_path_buf(&buf
, "config");
281 filemode
= TEST_FILEMODE
;
282 if (TEST_FILEMODE
&& !lstat(path
, &st1
)) {
284 filemode
= (!chmod(path
, st1
.st_mode
^ S_IXUSR
) &&
285 !lstat(path
, &st2
) &&
286 st1
.st_mode
!= st2
.st_mode
&&
287 !chmod(path
, st1
.st_mode
));
288 if (filemode
&& !reinit
&& (st1
.st_mode
& S_IXUSR
))
291 git_config_set("core.filemode", filemode
? "true" : "false");
293 if (is_bare_repository())
294 git_config_set("core.bare", "true");
296 git_config_set("core.bare", "false");
297 /* allow template config file to override the default */
298 if (log_all_ref_updates
== LOG_REFS_UNSET
)
299 git_config_set("core.logallrefupdates", "true");
300 if (needs_work_tree_config(original_git_dir
, work_tree
))
301 git_config_set("core.worktree", work_tree
);
305 /* Check if symlink is supported in the work tree */
306 path
= git_path_buf(&buf
, "tXXXXXX");
307 if (!close(xmkstemp(path
)) &&
309 !symlink("testing", path
) &&
310 !lstat(path
, &st1
) &&
311 S_ISLNK(st1
.st_mode
))
312 unlink(path
); /* good */
314 git_config_set("core.symlinks", "false");
316 /* Check if the filesystem is case-insensitive */
317 path
= git_path_buf(&buf
, "CoNfIg");
318 if (!access(path
, F_OK
))
319 git_config_set("core.ignorecase", "true");
320 probe_utf8_pathname_composition();
323 strbuf_release(&buf
);
327 static void create_object_directory(void)
329 struct strbuf path
= STRBUF_INIT
;
332 strbuf_addstr(&path
, get_object_directory());
335 safe_create_dir(path
.buf
, 1);
337 strbuf_setlen(&path
, baselen
);
338 strbuf_addstr(&path
, "/pack");
339 safe_create_dir(path
.buf
, 1);
341 strbuf_setlen(&path
, baselen
);
342 strbuf_addstr(&path
, "/info");
343 safe_create_dir(path
.buf
, 1);
345 strbuf_release(&path
);
348 static void separate_git_dir(const char *git_dir
, const char *git_link
)
352 if (!stat(git_link
, &st
)) {
355 if (S_ISREG(st
.st_mode
))
356 src
= read_gitfile(git_link
);
357 else if (S_ISDIR(st
.st_mode
))
360 die(_("unable to handle file type %d"), (int)st
.st_mode
);
362 if (rename(src
, git_dir
))
363 die_errno(_("unable to move %s to %s"), src
, git_dir
);
364 repair_worktrees(NULL
, NULL
);
367 write_file(git_link
, "gitdir: %s", git_dir
);
370 static void validate_hash_algorithm(struct repository_format
*repo_fmt
, int hash
)
372 const char *env
= getenv(GIT_DEFAULT_HASH_ENVIRONMENT
);
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.
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
;
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
;
390 int init_db(const char *git_dir
, const char *real_git_dir
,
391 const char *template_dir
, int hash
, const char *initial_branch
,
395 int exist_ok
= flags
& INIT_DB_EXIST_OK
;
396 char *original_git_dir
= real_pathdup(git_dir
, 1);
397 struct repository_format repo_fmt
= REPOSITORY_FORMAT_INIT
;
402 if (!exist_ok
&& !stat(git_dir
, &st
))
403 die(_("%s already exists"), git_dir
);
405 if (!exist_ok
&& !stat(real_git_dir
, &st
))
406 die(_("%s already exists"), real_git_dir
);
408 set_git_dir(real_git_dir
, 1);
409 git_dir
= get_git_dir();
410 separate_git_dir(git_dir
, original_git_dir
);
413 set_git_dir(git_dir
, 1);
414 git_dir
= get_git_dir();
416 startup_info
->have_repository
= 1;
418 /* Ensure `core.hidedotfiles` is processed */
419 git_config(platform_core_config
, NULL
);
421 safe_create_dir(git_dir
, 0);
423 init_is_bare_repository
= is_bare_repository();
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.
430 check_repository_format(&repo_fmt
);
432 validate_hash_algorithm(&repo_fmt
, hash
);
434 reinit
= create_default_files(template_dir
, original_git_dir
,
435 initial_branch
, &repo_fmt
,
436 flags
& INIT_DB_QUIET
);
437 if (reinit
&& initial_branch
)
438 warning(_("re-init: ignored --initial-branch=%s"),
441 create_object_directory();
443 if (get_shared_repository()) {
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
451 if (get_shared_repository() < 0)
452 /* force to the mode value */
453 xsnprintf(buf
, sizeof(buf
), "0%o", -get_shared_repository());
454 else if (get_shared_repository() == PERM_GROUP
)
455 xsnprintf(buf
, sizeof(buf
), "%d", OLD_PERM_GROUP
);
456 else if (get_shared_repository() == PERM_EVERYBODY
)
457 xsnprintf(buf
, sizeof(buf
), "%d", OLD_PERM_EVERYBODY
);
459 BUG("invalid value for shared_repository");
460 git_config_set("core.sharedrepository", buf
);
461 git_config_set("receive.denyNonFastforwards", "true");
464 if (!(flags
& INIT_DB_QUIET
)) {
465 int len
= strlen(git_dir
);
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] != '/' ? "/" : "");
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] != '/' ? "/" : "");
479 free(original_git_dir
);
483 static int guess_repository_type(const char *git_dir
)
490 * "GIT_DIR=. git init" is always bare.
491 * "GIT_DIR=`pwd` git init" too.
493 if (!strcmp(".", git_dir
))
496 cwd_is_git_dir
= !strcmp(git_dir
, cwd
);
501 * "GIT_DIR=.git or GIT_DIR=something/.git is usually not.
503 if (!strcmp(git_dir
, ".git"))
505 slash
= strrchr(git_dir
, '/');
506 if (slash
&& !strcmp(slash
, "/.git"))
510 * Otherwise it is often bare. At this point
511 * we are just guessing.
516 static int shared_callback(const struct option
*opt
, const char *arg
, int unset
)
518 BUG_ON_OPT_NEG(unset
);
519 *((int *) opt
->value
) = (arg
) ? git_config_perm("arg", arg
) : PERM_GROUP
;
523 static const char *const init_db_usage
[] = {
524 N_("git init [-q | --quiet] [--bare] [--template=<template-directory>]\n"
525 " [--separate-git-dir <git-dir>] [--object-format=<format>]\n"
526 " [-b <branch-name> | --initial-branch=<branch-name>]\n"
527 " [--shared[=<permissions>]] [<directory>]"),
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.
537 int cmd_init_db(int argc
, const char **argv
, const char *prefix
)
540 const char *real_git_dir
= NULL
;
541 const char *work_tree
;
542 const char *template_dir
= NULL
;
543 unsigned int flags
= 0;
544 const char *object_format
= NULL
;
545 const char *initial_branch
= NULL
;
546 int hash_algo
= GIT_HASH_UNKNOWN
;
547 const struct option init_db_options
[] = {
548 OPT_STRING(0, "template", &template_dir
, N_("template-directory"),
549 N_("directory from which templates will be used")),
550 OPT_SET_INT(0, "bare", &is_bare_repository_cfg
,
551 N_("create a bare repository"), 1),
552 { OPTION_CALLBACK
, 0, "shared", &init_shared_repository
,
554 N_("specify that the git repository is to be shared amongst several users"),
555 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
, shared_callback
, 0},
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")),
559 OPT_STRING('b', "initial-branch", &initial_branch
, N_("name"),
560 N_("override the name of the initial branch")),
561 OPT_STRING(0, "object-format", &object_format
, N_("hash"),
562 N_("specify the hash algorithm to use")),
566 argc
= parse_options(argc
, argv
, prefix
, init_db_options
, init_db_usage
, 0);
568 if (real_git_dir
&& is_bare_repository_cfg
== 1)
569 die(_("options '%s' and '%s' cannot be used together"), "--separate-git-dir", "--bare");
571 if (real_git_dir
&& !is_absolute_path(real_git_dir
))
572 real_git_dir
= real_pathdup(real_git_dir
, 1);
574 if (template_dir
&& *template_dir
&& !is_absolute_path(template_dir
)) {
575 template_dir
= absolute_pathdup(template_dir
);
576 UNLEAK(template_dir
);
582 if (chdir(argv
[0]) < 0) {
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.
590 saved
= get_shared_repository();
591 set_shared_repository(0);
592 switch (safe_create_leading_directories_const(argv
[0])) {
600 die_errno(_("cannot mkdir %s"), argv
[0]);
603 set_shared_repository(saved
);
604 if (mkdir(argv
[0], 0777) < 0)
605 die_errno(_("cannot mkdir %s"), argv
[0]);
609 die_errno(_("cannot chdir to %s"), argv
[0]);
611 } else if (0 < argc
) {
612 usage(init_db_usage
[0]);
614 if (is_bare_repository_cfg
== 1) {
615 char *cwd
= xgetcwd();
616 setenv(GIT_DIR_ENVIRONMENT
, cwd
, argc
> 0);
621 hash_algo
= hash_algo_by_name(object_format
);
622 if (hash_algo
== GIT_HASH_UNKNOWN
)
623 die(_("unknown hash algorithm '%s'"), object_format
);
626 if (init_shared_repository
!= -1)
627 set_shared_repository(init_shared_repository
);
630 * GIT_WORK_TREE makes sense only in conjunction with GIT_DIR
631 * without --bare. Catch the error early.
633 git_dir
= xstrdup_or_null(getenv(GIT_DIR_ENVIRONMENT
));
634 work_tree
= xstrdup_or_null(getenv(GIT_WORK_TREE_ENVIRONMENT
));
635 if ((!git_dir
|| is_bare_repository_cfg
== 1) && work_tree
)
636 die(_("%s (or --work-tree=<directory>) not allowed without "
637 "specifying %s (or --git-dir=<directory>)"),
638 GIT_WORK_TREE_ENVIRONMENT
,
639 GIT_DIR_ENVIRONMENT
);
642 * Set up the default .git directory contents
645 git_dir
= DEFAULT_GIT_DIR_ENVIRONMENT
;
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.
655 struct strbuf sb
= STRBUF_INIT
;
657 p
= read_gitfile_gently(git_dir
, &err
);
658 if (p
&& get_common_dir(&sb
, p
)) {
659 struct strbuf mainwt
= STRBUF_INIT
;
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
);
671 if (is_bare_repository_cfg
< 0)
672 is_bare_repository_cfg
= guess_repository_type(git_dir
);
674 if (!is_bare_repository_cfg
) {
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
);
678 git_work_tree_cfg
= real_pathdup(rel
, 1);
681 if (!git_work_tree_cfg
)
682 git_work_tree_cfg
= xgetcwd();
684 set_git_work_tree(work_tree
);
686 set_git_work_tree(git_work_tree_cfg
);
687 if (access(get_git_work_tree(), X_OK
))
688 die_errno (_("Cannot access work tree '%s'"),
689 get_git_work_tree());
693 die(_("--separate-git-dir incompatible with bare repository"));
695 set_git_work_tree(work_tree
);
698 UNLEAK(real_git_dir
);
702 flags
|= INIT_DB_EXIST_OK
;
703 return init_db(git_dir
, real_git_dir
, template_dir
, hash_algo
,
704 initial_branch
, flags
);