2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
12 #include "parse-options.h"
15 #ifndef DEFAULT_GIT_TEMPLATE_DIR
16 #define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates"
19 #ifdef NO_TRUSTABLE_FILEMODE
20 #define TEST_FILEMODE 0
22 #define TEST_FILEMODE 1
25 #define GIT_DEFAULT_HASH_ENVIRONMENT "GIT_DEFAULT_HASH"
27 static int init_is_bare_repository
= 0;
28 static int init_shared_repository
= -1;
30 static void copy_templates_1(struct strbuf
*path
, struct strbuf
*template_path
,
33 size_t path_baselen
= path
->len
;
34 size_t template_baselen
= template_path
->len
;
37 /* Note: if ".git/hooks" file exists in the repository being
38 * re-initialized, /etc/core-git/templates/hooks/update would
39 * cause "git init" to fail here. I think this is sane but
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.
44 safe_create_dir(path
->buf
, 1);
45 while ((de
= readdir(dir
)) != NULL
) {
46 struct stat st_git
, st_template
;
49 strbuf_setlen(path
, path_baselen
);
50 strbuf_setlen(template_path
, template_baselen
);
52 if (de
->d_name
[0] == '.')
54 strbuf_addstr(path
, de
->d_name
);
55 strbuf_addstr(template_path
, de
->d_name
);
56 if (lstat(path
->buf
, &st_git
)) {
58 die_errno(_("cannot stat '%s'"), path
->buf
);
63 if (lstat(template_path
->buf
, &st_template
))
64 die_errno(_("cannot stat template '%s'"), template_path
->buf
);
66 if (S_ISDIR(st_template
.st_mode
)) {
67 DIR *subdir
= opendir(template_path
->buf
);
69 die_errno(_("cannot opendir '%s'"), template_path
->buf
);
70 strbuf_addch(path
, '/');
71 strbuf_addch(template_path
, '/');
72 copy_templates_1(path
, template_path
, subdir
);
77 else if (S_ISLNK(st_template
.st_mode
)) {
78 struct strbuf lnk
= STRBUF_INIT
;
79 if (strbuf_readlink(&lnk
, template_path
->buf
,
80 st_template
.st_size
) < 0)
81 die_errno(_("cannot readlink '%s'"), template_path
->buf
);
82 if (symlink(lnk
.buf
, path
->buf
))
83 die_errno(_("cannot symlink '%s' '%s'"),
87 else if (S_ISREG(st_template
.st_mode
)) {
88 if (copy_file(path
->buf
, template_path
->buf
, st_template
.st_mode
))
89 die_errno(_("cannot copy '%s' to '%s'"),
90 template_path
->buf
, path
->buf
);
93 error(_("ignoring template %s"), template_path
->buf
);
97 static void copy_templates(const char *template_dir
, const char *init_template_dir
)
99 struct strbuf path
= STRBUF_INIT
;
100 struct strbuf template_path
= STRBUF_INIT
;
102 struct repository_format template_format
= REPOSITORY_FORMAT_INIT
;
103 struct strbuf err
= STRBUF_INIT
;
105 char *to_free
= NULL
;
108 template_dir
= getenv(TEMPLATE_DIR_ENVIRONMENT
);
110 template_dir
= init_template_dir
;
112 template_dir
= to_free
= system_path(DEFAULT_GIT_TEMPLATE_DIR
);
113 if (!template_dir
[0]) {
118 strbuf_addstr(&template_path
, template_dir
);
119 strbuf_complete(&template_path
, '/');
120 template_len
= template_path
.len
;
122 dir
= opendir(template_path
.buf
);
124 warning(_("templates not found in %s"), template_dir
);
128 /* Make sure that template is from the correct vintage */
129 strbuf_addstr(&template_path
, "config");
130 read_repository_format(&template_format
, template_path
.buf
);
131 strbuf_setlen(&template_path
, template_len
);
134 * No mention of version at all is OK, but anything else should be
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
);
142 goto close_free_return
;
145 strbuf_addstr(&path
, get_git_common_dir());
146 strbuf_complete(&path
, '/');
147 copy_templates_1(&path
, &template_path
, dir
);
152 strbuf_release(&path
);
153 strbuf_release(&template_path
);
154 clear_repository_format(&template_format
);
158 * If the git_dir is not directly inside the working tree, then git will not
159 * find it by default, and we need to set the worktree explicitly.
161 static int needs_work_tree_config(const char *git_dir
, const char *work_tree
)
163 if (!strcmp(work_tree
, "/") && !strcmp(git_dir
, "/.git"))
165 if (skip_prefix(git_dir
, work_tree
, &git_dir
) &&
166 !strcmp(git_dir
, "/.git"))
171 void initialize_repository_version(int hash_algo
, int reinit
)
173 char repo_version_string
[10];
174 int repo_version
= GIT_REPO_VERSION
;
176 if (hash_algo
!= GIT_HASH_SHA1
)
177 repo_version
= GIT_REPO_VERSION_READ
;
179 /* This forces creation of new config file */
180 xsnprintf(repo_version_string
, sizeof(repo_version_string
),
182 git_config_set("core.repositoryformatversion", repo_version_string
);
184 if (hash_algo
!= GIT_HASH_SHA1
)
185 git_config_set("extensions.objectformat",
186 hash_algos
[hash_algo
].name
);
188 git_config_set_gently("extensions.objectformat", NULL
);
191 static int create_default_files(const char *template_path
,
192 const char *original_git_dir
,
193 const char *initial_branch
,
194 const struct repository_format
*fmt
,
198 struct strbuf buf
= STRBUF_INIT
;
203 struct strbuf err
= STRBUF_INIT
;
204 const char *init_template_dir
= NULL
;
205 const char *work_tree
= get_git_work_tree();
208 * First copy the templates -- we might have the default
209 * config file there, in which case we would want to read
210 * from it after installing.
212 * Before reading that config, we also need to clear out any cached
213 * values (since we've just potentially changed what's available on
216 git_config_get_pathname("init.templatedir", &init_template_dir
);
217 copy_templates(template_path
, init_template_dir
);
218 free((char *)init_template_dir
);
220 reset_shared_repository();
221 git_config(git_default_config
, NULL
);
224 * We must make sure command-line options continue to override any
225 * values we might have just re-read from the config.
227 is_bare_repository_cfg
= init_is_bare_repository
|| !work_tree
;
228 if (init_shared_repository
!= -1)
229 set_shared_repository(init_shared_repository
);
232 * We would have created the above under user's umask -- under
233 * shared-repository settings, we would need to fix them up.
235 if (get_shared_repository()) {
236 adjust_shared_perm(get_git_dir());
240 * We need to create a "refs" dir in any case so that older
241 * versions of git can tell that this is a repository.
243 safe_create_dir(git_path("refs"), 1);
244 adjust_shared_perm(git_path("refs"));
246 if (refs_init_db(&err
))
247 die("failed to set up refs db: %s", err
.buf
);
250 * Point the HEAD symref to the initial branch with if HEAD does
253 path
= git_path_buf(&buf
, "HEAD");
254 reinit
= (!access(path
, R_OK
)
255 || readlink(path
, junk
, sizeof(junk
)-1) != -1);
260 initial_branch
= git_default_branch_name(quiet
);
262 ref
= xstrfmt("refs/heads/%s", initial_branch
);
263 if (check_refname_format(ref
, 0) < 0)
264 die(_("invalid initial branch name: '%s'"),
267 if (create_symref("HEAD", ref
, NULL
) < 0)
272 initialize_repository_version(fmt
->hash_algo
, 0);
274 /* Check filemode trustability */
275 path
= git_path_buf(&buf
, "config");
276 filemode
= TEST_FILEMODE
;
277 if (TEST_FILEMODE
&& !lstat(path
, &st1
)) {
279 filemode
= (!chmod(path
, st1
.st_mode
^ S_IXUSR
) &&
280 !lstat(path
, &st2
) &&
281 st1
.st_mode
!= st2
.st_mode
&&
282 !chmod(path
, st1
.st_mode
));
283 if (filemode
&& !reinit
&& (st1
.st_mode
& S_IXUSR
))
286 git_config_set("core.filemode", filemode
? "true" : "false");
288 if (is_bare_repository())
289 git_config_set("core.bare", "true");
291 git_config_set("core.bare", "false");
292 /* allow template config file to override the default */
293 if (log_all_ref_updates
== LOG_REFS_UNSET
)
294 git_config_set("core.logallrefupdates", "true");
295 if (needs_work_tree_config(original_git_dir
, work_tree
))
296 git_config_set("core.worktree", work_tree
);
300 /* Check if symlink is supported in the work tree */
301 path
= git_path_buf(&buf
, "tXXXXXX");
302 if (!close(xmkstemp(path
)) &&
304 !symlink("testing", path
) &&
305 !lstat(path
, &st1
) &&
306 S_ISLNK(st1
.st_mode
))
307 unlink(path
); /* good */
309 git_config_set("core.symlinks", "false");
311 /* Check if the filesystem is case-insensitive */
312 path
= git_path_buf(&buf
, "CoNfIg");
313 if (!access(path
, F_OK
))
314 git_config_set("core.ignorecase", "true");
315 probe_utf8_pathname_composition();
318 strbuf_release(&buf
);
322 static void create_object_directory(void)
324 struct strbuf path
= STRBUF_INIT
;
327 strbuf_addstr(&path
, get_object_directory());
330 safe_create_dir(path
.buf
, 1);
332 strbuf_setlen(&path
, baselen
);
333 strbuf_addstr(&path
, "/pack");
334 safe_create_dir(path
.buf
, 1);
336 strbuf_setlen(&path
, baselen
);
337 strbuf_addstr(&path
, "/info");
338 safe_create_dir(path
.buf
, 1);
340 strbuf_release(&path
);
343 static void separate_git_dir(const char *git_dir
, const char *git_link
)
347 if (!stat(git_link
, &st
)) {
350 if (S_ISREG(st
.st_mode
))
351 src
= read_gitfile(git_link
);
352 else if (S_ISDIR(st
.st_mode
))
355 die(_("unable to handle file type %d"), (int)st
.st_mode
);
357 if (rename(src
, git_dir
))
358 die_errno(_("unable to move %s to %s"), src
, git_dir
);
359 repair_worktrees(NULL
, NULL
);
362 write_file(git_link
, "gitdir: %s", git_dir
);
365 static void validate_hash_algorithm(struct repository_format
*repo_fmt
, int hash
)
367 const char *env
= getenv(GIT_DEFAULT_HASH_ENVIRONMENT
);
369 * If we already have an initialized repo, don't allow the user to
370 * specify a different algorithm, as that could cause corruption.
371 * Otherwise, if the user has specified one on the command line, use it.
373 if (repo_fmt
->version
>= 0 && hash
!= GIT_HASH_UNKNOWN
&& hash
!= repo_fmt
->hash_algo
)
374 die(_("attempt to reinitialize repository with different hash"));
375 else if (hash
!= GIT_HASH_UNKNOWN
)
376 repo_fmt
->hash_algo
= hash
;
378 int env_algo
= hash_algo_by_name(env
);
379 if (env_algo
== GIT_HASH_UNKNOWN
)
380 die(_("unknown hash algorithm '%s'"), env
);
381 repo_fmt
->hash_algo
= env_algo
;
385 int init_db(const char *git_dir
, const char *real_git_dir
,
386 const char *template_dir
, int hash
, const char *initial_branch
,
390 int exist_ok
= flags
& INIT_DB_EXIST_OK
;
391 char *original_git_dir
= real_pathdup(git_dir
, 1);
392 struct repository_format repo_fmt
= REPOSITORY_FORMAT_INIT
;
397 if (!exist_ok
&& !stat(git_dir
, &st
))
398 die(_("%s already exists"), git_dir
);
400 if (!exist_ok
&& !stat(real_git_dir
, &st
))
401 die(_("%s already exists"), real_git_dir
);
403 set_git_dir(real_git_dir
, 1);
404 git_dir
= get_git_dir();
405 separate_git_dir(git_dir
, original_git_dir
);
408 set_git_dir(git_dir
, 1);
409 git_dir
= get_git_dir();
411 startup_info
->have_repository
= 1;
413 /* Ensure `core.hidedotfiles` is processed */
414 git_config(platform_core_config
, NULL
);
416 safe_create_dir(git_dir
, 0);
418 init_is_bare_repository
= is_bare_repository();
420 /* Check to see if the repository version is right.
421 * Note that a newly created repository does not have
422 * config file, so this will not fail. What we are catching
423 * is an attempt to reinitialize new repository with an old tool.
425 check_repository_format(&repo_fmt
);
427 validate_hash_algorithm(&repo_fmt
, hash
);
429 reinit
= create_default_files(template_dir
, original_git_dir
,
430 initial_branch
, &repo_fmt
,
431 flags
& INIT_DB_QUIET
);
432 if (reinit
&& initial_branch
)
433 warning(_("re-init: ignored --initial-branch=%s"),
436 create_object_directory();
438 if (get_shared_repository()) {
440 /* We do not spell "group" and such, so that
441 * the configuration can be read by older version
442 * of git. Note, we use octal numbers for new share modes,
443 * and compatibility values for PERM_GROUP and
446 if (get_shared_repository() < 0)
447 /* force to the mode value */
448 xsnprintf(buf
, sizeof(buf
), "0%o", -get_shared_repository());
449 else if (get_shared_repository() == PERM_GROUP
)
450 xsnprintf(buf
, sizeof(buf
), "%d", OLD_PERM_GROUP
);
451 else if (get_shared_repository() == PERM_EVERYBODY
)
452 xsnprintf(buf
, sizeof(buf
), "%d", OLD_PERM_EVERYBODY
);
454 BUG("invalid value for shared_repository");
455 git_config_set("core.sharedrepository", buf
);
456 git_config_set("receive.denyNonFastforwards", "true");
459 if (!(flags
& INIT_DB_QUIET
)) {
460 int len
= strlen(git_dir
);
463 printf(get_shared_repository()
464 ? _("Reinitialized existing shared Git repository in %s%s\n")
465 : _("Reinitialized existing Git repository in %s%s\n"),
466 git_dir
, len
&& git_dir
[len
-1] != '/' ? "/" : "");
468 printf(get_shared_repository()
469 ? _("Initialized empty shared Git repository in %s%s\n")
470 : _("Initialized empty Git repository in %s%s\n"),
471 git_dir
, len
&& git_dir
[len
-1] != '/' ? "/" : "");
474 free(original_git_dir
);
478 static int guess_repository_type(const char *git_dir
)
485 * "GIT_DIR=. git init" is always bare.
486 * "GIT_DIR=`pwd` git init" too.
488 if (!strcmp(".", git_dir
))
491 cwd_is_git_dir
= !strcmp(git_dir
, cwd
);
496 * "GIT_DIR=.git or GIT_DIR=something/.git is usually not.
498 if (!strcmp(git_dir
, ".git"))
500 slash
= strrchr(git_dir
, '/');
501 if (slash
&& !strcmp(slash
, "/.git"))
505 * Otherwise it is often bare. At this point
506 * we are just guessing.
511 static int shared_callback(const struct option
*opt
, const char *arg
, int unset
)
513 BUG_ON_OPT_NEG(unset
);
514 *((int *) opt
->value
) = (arg
) ? git_config_perm("arg", arg
) : PERM_GROUP
;
518 static const char *const init_db_usage
[] = {
519 N_("git init [-q | --quiet] [--bare] [--template=<template-directory>]\n"
520 " [--separate-git-dir <git-dir>] [--object-format=<format>]\n"
521 " [-b <branch-name> | --initial-branch=<branch-name>]\n"
522 " [--shared[=<permissions>]] [<directory>]"),
527 * If you want to, you can share the DB area with any number of branches.
528 * That has advantages: you can save space by sharing all the SHA1 objects.
529 * On the other hand, it might just make lookup slower and messier. You
530 * be the judge. The default case is to have one DB per managed directory.
532 int cmd_init_db(int argc
, const char **argv
, const char *prefix
)
535 const char *real_git_dir
= NULL
;
536 const char *work_tree
;
537 const char *template_dir
= NULL
;
538 unsigned int flags
= 0;
539 const char *object_format
= NULL
;
540 const char *initial_branch
= NULL
;
541 int hash_algo
= GIT_HASH_UNKNOWN
;
542 const struct option init_db_options
[] = {
543 OPT_STRING(0, "template", &template_dir
, N_("template-directory"),
544 N_("directory from which templates will be used")),
545 OPT_SET_INT(0, "bare", &is_bare_repository_cfg
,
546 N_("create a bare repository"), 1),
547 { OPTION_CALLBACK
, 0, "shared", &init_shared_repository
,
549 N_("specify that the git repository is to be shared amongst several users"),
550 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
, shared_callback
, 0},
551 OPT_BIT('q', "quiet", &flags
, N_("be quiet"), INIT_DB_QUIET
),
552 OPT_STRING(0, "separate-git-dir", &real_git_dir
, N_("gitdir"),
553 N_("separate git dir from working tree")),
554 OPT_STRING('b', "initial-branch", &initial_branch
, N_("name"),
555 N_("override the name of the initial branch")),
556 OPT_STRING(0, "object-format", &object_format
, N_("hash"),
557 N_("specify the hash algorithm to use")),
561 argc
= parse_options(argc
, argv
, prefix
, init_db_options
, init_db_usage
, 0);
563 if (real_git_dir
&& is_bare_repository_cfg
== 1)
564 die(_("options '%s' and '%s' cannot be used together"), "--separate-git-dir", "--bare");
566 if (real_git_dir
&& !is_absolute_path(real_git_dir
))
567 real_git_dir
= real_pathdup(real_git_dir
, 1);
569 if (template_dir
&& *template_dir
&& !is_absolute_path(template_dir
)) {
570 template_dir
= absolute_pathdup(template_dir
);
571 UNLEAK(template_dir
);
577 if (chdir(argv
[0]) < 0) {
581 * At this point we haven't read any configuration,
582 * and we know shared_repository should always be 0;
583 * but just in case we play safe.
585 saved
= get_shared_repository();
586 set_shared_repository(0);
587 switch (safe_create_leading_directories_const(argv
[0])) {
595 die_errno(_("cannot mkdir %s"), argv
[0]);
598 set_shared_repository(saved
);
599 if (mkdir(argv
[0], 0777) < 0)
600 die_errno(_("cannot mkdir %s"), argv
[0]);
604 die_errno(_("cannot chdir to %s"), argv
[0]);
606 } else if (0 < argc
) {
607 usage(init_db_usage
[0]);
609 if (is_bare_repository_cfg
== 1) {
610 char *cwd
= xgetcwd();
611 setenv(GIT_DIR_ENVIRONMENT
, cwd
, argc
> 0);
616 hash_algo
= hash_algo_by_name(object_format
);
617 if (hash_algo
== GIT_HASH_UNKNOWN
)
618 die(_("unknown hash algorithm '%s'"), object_format
);
621 if (init_shared_repository
!= -1)
622 set_shared_repository(init_shared_repository
);
625 * GIT_WORK_TREE makes sense only in conjunction with GIT_DIR
626 * without --bare. Catch the error early.
628 git_dir
= xstrdup_or_null(getenv(GIT_DIR_ENVIRONMENT
));
629 work_tree
= xstrdup_or_null(getenv(GIT_WORK_TREE_ENVIRONMENT
));
630 if ((!git_dir
|| is_bare_repository_cfg
== 1) && work_tree
)
631 die(_("%s (or --work-tree=<directory>) not allowed without "
632 "specifying %s (or --git-dir=<directory>)"),
633 GIT_WORK_TREE_ENVIRONMENT
,
634 GIT_DIR_ENVIRONMENT
);
637 * Set up the default .git directory contents
640 git_dir
= DEFAULT_GIT_DIR_ENVIRONMENT
;
643 * When --separate-git-dir is used inside a linked worktree, take
644 * care to ensure that the common .git/ directory is relocated, not
645 * the worktree-specific .git/worktrees/<id>/ directory.
650 struct strbuf sb
= STRBUF_INIT
;
652 p
= read_gitfile_gently(git_dir
, &err
);
653 if (p
&& get_common_dir(&sb
, p
)) {
654 struct strbuf mainwt
= STRBUF_INIT
;
656 strbuf_addbuf(&mainwt
, &sb
);
657 strbuf_strip_suffix(&mainwt
, "/.git");
658 if (chdir(mainwt
.buf
) < 0)
659 die_errno(_("cannot chdir to %s"), mainwt
.buf
);
660 strbuf_release(&mainwt
);
661 git_dir
= strbuf_detach(&sb
, NULL
);
666 if (is_bare_repository_cfg
< 0)
667 is_bare_repository_cfg
= guess_repository_type(git_dir
);
669 if (!is_bare_repository_cfg
) {
670 const char *git_dir_parent
= strrchr(git_dir
, '/');
671 if (git_dir_parent
) {
672 char *rel
= xstrndup(git_dir
, git_dir_parent
- git_dir
);
673 git_work_tree_cfg
= real_pathdup(rel
, 1);
676 if (!git_work_tree_cfg
)
677 git_work_tree_cfg
= xgetcwd();
679 set_git_work_tree(work_tree
);
681 set_git_work_tree(git_work_tree_cfg
);
682 if (access(get_git_work_tree(), X_OK
))
683 die_errno (_("Cannot access work tree '%s'"),
684 get_git_work_tree());
688 die(_("--separate-git-dir incompatible with bare repository"));
690 set_git_work_tree(work_tree
);
693 UNLEAK(real_git_dir
);
697 flags
|= INIT_DB_EXIST_OK
;
698 return init_db(git_dir
, real_git_dir
, template_dir
, hash_algo
,
699 initial_branch
, flags
);