2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
11 #include "parse-options.h"
14 #ifndef DEFAULT_GIT_TEMPLATE_DIR
15 #define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates"
18 #ifdef NO_TRUSTABLE_FILEMODE
19 #define TEST_FILEMODE 0
21 #define TEST_FILEMODE 1
24 #define GIT_DEFAULT_HASH_ENVIRONMENT "GIT_DEFAULT_HASH"
26 static int init_is_bare_repository
= 0;
27 static int init_shared_repository
= -1;
29 static void copy_templates_1(struct strbuf
*path
, struct strbuf
*template_path
,
32 size_t path_baselen
= path
->len
;
33 size_t template_baselen
= template_path
->len
;
36 /* Note: if ".git/hooks" file exists in the repository being
37 * re-initialized, /etc/core-git/templates/hooks/update would
38 * cause "git init" to fail here. I think this is sane but
39 * it means that the set of templates we ship by default, along
40 * with the way the namespace under .git/ is organized, should
41 * be really carefully chosen.
43 safe_create_dir(path
->buf
, 1);
44 while ((de
= readdir(dir
)) != NULL
) {
45 struct stat st_git
, st_template
;
48 strbuf_setlen(path
, path_baselen
);
49 strbuf_setlen(template_path
, template_baselen
);
51 if (de
->d_name
[0] == '.')
53 strbuf_addstr(path
, de
->d_name
);
54 strbuf_addstr(template_path
, de
->d_name
);
55 if (lstat(path
->buf
, &st_git
)) {
57 die_errno(_("cannot stat '%s'"), path
->buf
);
62 if (lstat(template_path
->buf
, &st_template
))
63 die_errno(_("cannot stat template '%s'"), template_path
->buf
);
65 if (S_ISDIR(st_template
.st_mode
)) {
66 DIR *subdir
= opendir(template_path
->buf
);
68 die_errno(_("cannot opendir '%s'"), template_path
->buf
);
69 strbuf_addch(path
, '/');
70 strbuf_addch(template_path
, '/');
71 copy_templates_1(path
, template_path
, subdir
);
76 else if (S_ISLNK(st_template
.st_mode
)) {
77 struct strbuf lnk
= STRBUF_INIT
;
78 if (strbuf_readlink(&lnk
, template_path
->buf
,
79 st_template
.st_size
) < 0)
80 die_errno(_("cannot readlink '%s'"), template_path
->buf
);
81 if (symlink(lnk
.buf
, path
->buf
))
82 die_errno(_("cannot symlink '%s' '%s'"),
86 else if (S_ISREG(st_template
.st_mode
)) {
87 if (copy_file(path
->buf
, template_path
->buf
, st_template
.st_mode
))
88 die_errno(_("cannot copy '%s' to '%s'"),
89 template_path
->buf
, path
->buf
);
92 error(_("ignoring template %s"), template_path
->buf
);
96 static void copy_templates(const char *template_dir
, const char *init_template_dir
)
98 struct strbuf path
= STRBUF_INIT
;
99 struct strbuf template_path
= STRBUF_INIT
;
101 struct repository_format template_format
= REPOSITORY_FORMAT_INIT
;
102 struct strbuf err
= STRBUF_INIT
;
104 char *to_free
= NULL
;
107 template_dir
= getenv(TEMPLATE_DIR_ENVIRONMENT
);
109 template_dir
= init_template_dir
;
111 template_dir
= to_free
= system_path(DEFAULT_GIT_TEMPLATE_DIR
);
112 if (!template_dir
[0]) {
117 strbuf_addstr(&template_path
, template_dir
);
118 strbuf_complete(&template_path
, '/');
119 template_len
= template_path
.len
;
121 dir
= opendir(template_path
.buf
);
123 warning(_("templates not found in %s"), template_dir
);
127 /* Make sure that template is from the correct vintage */
128 strbuf_addstr(&template_path
, "config");
129 read_repository_format(&template_format
, template_path
.buf
);
130 strbuf_setlen(&template_path
, template_len
);
133 * No mention of version at all is OK, but anything else should be
136 if (template_format
.version
>= 0 &&
137 verify_repository_format(&template_format
, &err
) < 0) {
138 warning(_("not copying templates from '%s': %s"),
139 template_dir
, err
.buf
);
140 strbuf_release(&err
);
141 goto close_free_return
;
144 strbuf_addstr(&path
, get_git_common_dir());
145 strbuf_complete(&path
, '/');
146 copy_templates_1(&path
, &template_path
, dir
);
151 strbuf_release(&path
);
152 strbuf_release(&template_path
);
153 clear_repository_format(&template_format
);
157 * If the git_dir is not directly inside the working tree, then git will not
158 * find it by default, and we need to set the worktree explicitly.
160 static int needs_work_tree_config(const char *git_dir
, const char *work_tree
)
162 if (!strcmp(work_tree
, "/") && !strcmp(git_dir
, "/.git"))
164 if (skip_prefix(git_dir
, work_tree
, &git_dir
) &&
165 !strcmp(git_dir
, "/.git"))
170 void initialize_repository_version(int hash_algo
, int reinit
)
172 char repo_version_string
[10];
173 int repo_version
= GIT_REPO_VERSION
;
175 if (hash_algo
!= GIT_HASH_SHA1
)
176 repo_version
= GIT_REPO_VERSION_READ
;
178 /* This forces creation of new config file */
179 xsnprintf(repo_version_string
, sizeof(repo_version_string
),
181 git_config_set("core.repositoryformatversion", repo_version_string
);
183 if (hash_algo
!= GIT_HASH_SHA1
)
184 git_config_set("extensions.objectformat",
185 hash_algos
[hash_algo
].name
);
187 git_config_set_gently("extensions.objectformat", NULL
);
190 static int create_default_files(const char *template_path
,
191 const char *original_git_dir
,
192 const char *initial_branch
,
193 const struct repository_format
*fmt
,
197 struct strbuf buf
= STRBUF_INIT
;
202 struct strbuf err
= STRBUF_INIT
;
203 const char *init_template_dir
= NULL
;
204 const char *work_tree
= get_git_work_tree();
207 * First copy the templates -- we might have the default
208 * config file there, in which case we would want to read
209 * from it after installing.
211 * Before reading that config, we also need to clear out any cached
212 * values (since we've just potentially changed what's available on
215 git_config_get_pathname("init.templatedir", &init_template_dir
);
216 copy_templates(template_path
, init_template_dir
);
217 free((char *)init_template_dir
);
219 reset_shared_repository();
220 git_config(git_default_config
, NULL
);
223 * We must make sure command-line options continue to override any
224 * values we might have just re-read from the config.
226 is_bare_repository_cfg
= init_is_bare_repository
|| !work_tree
;
227 if (init_shared_repository
!= -1)
228 set_shared_repository(init_shared_repository
);
231 * We would have created the above under user's umask -- under
232 * shared-repository settings, we would need to fix them up.
234 if (get_shared_repository()) {
235 adjust_shared_perm(get_git_dir());
239 * We need to create a "refs" dir in any case so that older
240 * versions of git can tell that this is a repository.
242 safe_create_dir(git_path("refs"), 1);
243 adjust_shared_perm(git_path("refs"));
245 if (refs_init_db(&err
))
246 die("failed to set up refs db: %s", err
.buf
);
249 * Point the HEAD symref to the initial branch with if HEAD does
252 path
= git_path_buf(&buf
, "HEAD");
253 reinit
= (!access(path
, R_OK
)
254 || readlink(path
, junk
, sizeof(junk
)-1) != -1);
259 initial_branch
= git_default_branch_name(quiet
);
261 ref
= xstrfmt("refs/heads/%s", initial_branch
);
262 if (check_refname_format(ref
, 0) < 0)
263 die(_("invalid initial branch name: '%s'"),
266 if (create_symref("HEAD", ref
, NULL
) < 0)
271 initialize_repository_version(fmt
->hash_algo
, 0);
273 /* Check filemode trustability */
274 path
= git_path_buf(&buf
, "config");
275 filemode
= TEST_FILEMODE
;
276 if (TEST_FILEMODE
&& !lstat(path
, &st1
)) {
278 filemode
= (!chmod(path
, st1
.st_mode
^ S_IXUSR
) &&
279 !lstat(path
, &st2
) &&
280 st1
.st_mode
!= st2
.st_mode
&&
281 !chmod(path
, st1
.st_mode
));
282 if (filemode
&& !reinit
&& (st1
.st_mode
& S_IXUSR
))
285 git_config_set("core.filemode", filemode
? "true" : "false");
287 if (is_bare_repository())
288 git_config_set("core.bare", "true");
290 git_config_set("core.bare", "false");
291 /* allow template config file to override the default */
292 if (log_all_ref_updates
== LOG_REFS_UNSET
)
293 git_config_set("core.logallrefupdates", "true");
294 if (needs_work_tree_config(original_git_dir
, work_tree
))
295 git_config_set("core.worktree", work_tree
);
299 /* Check if symlink is supported in the work tree */
300 path
= git_path_buf(&buf
, "tXXXXXX");
301 if (!close(xmkstemp(path
)) &&
303 !symlink("testing", path
) &&
304 !lstat(path
, &st1
) &&
305 S_ISLNK(st1
.st_mode
))
306 unlink(path
); /* good */
308 git_config_set("core.symlinks", "false");
310 /* Check if the filesystem is case-insensitive */
311 path
= git_path_buf(&buf
, "CoNfIg");
312 if (!access(path
, F_OK
))
313 git_config_set("core.ignorecase", "true");
314 probe_utf8_pathname_composition();
317 strbuf_release(&buf
);
321 static void create_object_directory(void)
323 struct strbuf path
= STRBUF_INIT
;
326 strbuf_addstr(&path
, get_object_directory());
329 safe_create_dir(path
.buf
, 1);
331 strbuf_setlen(&path
, baselen
);
332 strbuf_addstr(&path
, "/pack");
333 safe_create_dir(path
.buf
, 1);
335 strbuf_setlen(&path
, baselen
);
336 strbuf_addstr(&path
, "/info");
337 safe_create_dir(path
.buf
, 1);
339 strbuf_release(&path
);
342 static void separate_git_dir(const char *git_dir
, const char *git_link
)
346 if (!stat(git_link
, &st
)) {
349 if (S_ISREG(st
.st_mode
))
350 src
= read_gitfile(git_link
);
351 else if (S_ISDIR(st
.st_mode
))
354 die(_("unable to handle file type %d"), (int)st
.st_mode
);
356 if (rename(src
, git_dir
))
357 die_errno(_("unable to move %s to %s"), src
, git_dir
);
358 repair_worktrees(NULL
, NULL
);
361 write_file(git_link
, "gitdir: %s", git_dir
);
364 static void validate_hash_algorithm(struct repository_format
*repo_fmt
, int hash
)
366 const char *env
= getenv(GIT_DEFAULT_HASH_ENVIRONMENT
);
368 * If we already have an initialized repo, don't allow the user to
369 * specify a different algorithm, as that could cause corruption.
370 * Otherwise, if the user has specified one on the command line, use it.
372 if (repo_fmt
->version
>= 0 && hash
!= GIT_HASH_UNKNOWN
&& hash
!= repo_fmt
->hash_algo
)
373 die(_("attempt to reinitialize repository with different hash"));
374 else if (hash
!= GIT_HASH_UNKNOWN
)
375 repo_fmt
->hash_algo
= hash
;
377 int env_algo
= hash_algo_by_name(env
);
378 if (env_algo
== GIT_HASH_UNKNOWN
)
379 die(_("unknown hash algorithm '%s'"), env
);
380 repo_fmt
->hash_algo
= env_algo
;
384 int init_db(const char *git_dir
, const char *real_git_dir
,
385 const char *template_dir
, int hash
, const char *initial_branch
,
389 int exist_ok
= flags
& INIT_DB_EXIST_OK
;
390 char *original_git_dir
= real_pathdup(git_dir
, 1);
391 struct repository_format repo_fmt
= REPOSITORY_FORMAT_INIT
;
396 if (!exist_ok
&& !stat(git_dir
, &st
))
397 die(_("%s already exists"), git_dir
);
399 if (!exist_ok
&& !stat(real_git_dir
, &st
))
400 die(_("%s already exists"), real_git_dir
);
402 set_git_dir(real_git_dir
, 1);
403 git_dir
= get_git_dir();
404 separate_git_dir(git_dir
, original_git_dir
);
407 set_git_dir(git_dir
, 1);
408 git_dir
= get_git_dir();
410 startup_info
->have_repository
= 1;
412 /* Ensure `core.hidedotfiles` is processed */
413 git_config(platform_core_config
, NULL
);
415 safe_create_dir(git_dir
, 0);
417 init_is_bare_repository
= is_bare_repository();
419 /* Check to see if the repository version is right.
420 * Note that a newly created repository does not have
421 * config file, so this will not fail. What we are catching
422 * is an attempt to reinitialize new repository with an old tool.
424 check_repository_format(&repo_fmt
);
426 validate_hash_algorithm(&repo_fmt
, hash
);
428 reinit
= create_default_files(template_dir
, original_git_dir
,
429 initial_branch
, &repo_fmt
,
430 flags
& INIT_DB_QUIET
);
431 if (reinit
&& initial_branch
)
432 warning(_("re-init: ignored --initial-branch=%s"),
435 create_object_directory();
437 if (get_shared_repository()) {
439 /* We do not spell "group" and such, so that
440 * the configuration can be read by older version
441 * of git. Note, we use octal numbers for new share modes,
442 * and compatibility values for PERM_GROUP and
445 if (get_shared_repository() < 0)
446 /* force to the mode value */
447 xsnprintf(buf
, sizeof(buf
), "0%o", -get_shared_repository());
448 else if (get_shared_repository() == PERM_GROUP
)
449 xsnprintf(buf
, sizeof(buf
), "%d", OLD_PERM_GROUP
);
450 else if (get_shared_repository() == PERM_EVERYBODY
)
451 xsnprintf(buf
, sizeof(buf
), "%d", OLD_PERM_EVERYBODY
);
453 BUG("invalid value for shared_repository");
454 git_config_set("core.sharedrepository", buf
);
455 git_config_set("receive.denyNonFastforwards", "true");
458 if (!(flags
& INIT_DB_QUIET
)) {
459 int len
= strlen(git_dir
);
462 printf(get_shared_repository()
463 ? _("Reinitialized existing shared Git repository in %s%s\n")
464 : _("Reinitialized existing Git repository in %s%s\n"),
465 git_dir
, len
&& git_dir
[len
-1] != '/' ? "/" : "");
467 printf(get_shared_repository()
468 ? _("Initialized empty shared Git repository in %s%s\n")
469 : _("Initialized empty Git repository in %s%s\n"),
470 git_dir
, len
&& git_dir
[len
-1] != '/' ? "/" : "");
473 free(original_git_dir
);
477 static int guess_repository_type(const char *git_dir
)
484 * "GIT_DIR=. git init" is always bare.
485 * "GIT_DIR=`pwd` git init" too.
487 if (!strcmp(".", git_dir
))
490 cwd_is_git_dir
= !strcmp(git_dir
, cwd
);
495 * "GIT_DIR=.git or GIT_DIR=something/.git is usually not.
497 if (!strcmp(git_dir
, ".git"))
499 slash
= strrchr(git_dir
, '/');
500 if (slash
&& !strcmp(slash
, "/.git"))
504 * Otherwise it is often bare. At this point
505 * we are just guessing.
510 static int shared_callback(const struct option
*opt
, const char *arg
, int unset
)
512 BUG_ON_OPT_NEG(unset
);
513 *((int *) opt
->value
) = (arg
) ? git_config_perm("arg", arg
) : PERM_GROUP
;
517 static const char *const init_db_usage
[] = {
518 N_("git init [-q | --quiet] [--bare] [--template=<template-directory>]\n"
519 " [--separate-git-dir <git-dir>] [--object-format=<format>]\n"
520 " [-b <branch-name> | --initial-branch=<branch-name>]\n"
521 " [--shared[=<permissions>]] [<directory>]"),
526 * If you want to, you can share the DB area with any number of branches.
527 * That has advantages: you can save space by sharing all the SHA1 objects.
528 * On the other hand, it might just make lookup slower and messier. You
529 * be the judge. The default case is to have one DB per managed directory.
531 int cmd_init_db(int argc
, const char **argv
, const char *prefix
)
534 const char *real_git_dir
= NULL
;
535 const char *work_tree
;
536 const char *template_dir
= NULL
;
537 unsigned int flags
= 0;
538 const char *object_format
= NULL
;
539 const char *initial_branch
= NULL
;
540 int hash_algo
= GIT_HASH_UNKNOWN
;
541 const struct option init_db_options
[] = {
542 OPT_STRING(0, "template", &template_dir
, N_("template-directory"),
543 N_("directory from which templates will be used")),
544 OPT_SET_INT(0, "bare", &is_bare_repository_cfg
,
545 N_("create a bare repository"), 1),
546 { OPTION_CALLBACK
, 0, "shared", &init_shared_repository
,
548 N_("specify that the git repository is to be shared amongst several users"),
549 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
, shared_callback
, 0},
550 OPT_BIT('q', "quiet", &flags
, N_("be quiet"), INIT_DB_QUIET
),
551 OPT_STRING(0, "separate-git-dir", &real_git_dir
, N_("gitdir"),
552 N_("separate git dir from working tree")),
553 OPT_STRING('b', "initial-branch", &initial_branch
, N_("name"),
554 N_("override the name of the initial branch")),
555 OPT_STRING(0, "object-format", &object_format
, N_("hash"),
556 N_("specify the hash algorithm to use")),
560 argc
= parse_options(argc
, argv
, prefix
, init_db_options
, init_db_usage
, 0);
562 if (real_git_dir
&& is_bare_repository_cfg
== 1)
563 die(_("options '%s' and '%s' cannot be used together"), "--separate-git-dir", "--bare");
565 if (real_git_dir
&& !is_absolute_path(real_git_dir
))
566 real_git_dir
= real_pathdup(real_git_dir
, 1);
568 if (template_dir
&& *template_dir
&& !is_absolute_path(template_dir
)) {
569 template_dir
= absolute_pathdup(template_dir
);
570 UNLEAK(template_dir
);
576 if (chdir(argv
[0]) < 0) {
580 * At this point we haven't read any configuration,
581 * and we know shared_repository should always be 0;
582 * but just in case we play safe.
584 saved
= get_shared_repository();
585 set_shared_repository(0);
586 switch (safe_create_leading_directories_const(argv
[0])) {
594 die_errno(_("cannot mkdir %s"), argv
[0]);
597 set_shared_repository(saved
);
598 if (mkdir(argv
[0], 0777) < 0)
599 die_errno(_("cannot mkdir %s"), argv
[0]);
603 die_errno(_("cannot chdir to %s"), argv
[0]);
605 } else if (0 < argc
) {
606 usage(init_db_usage
[0]);
608 if (is_bare_repository_cfg
== 1) {
609 char *cwd
= xgetcwd();
610 setenv(GIT_DIR_ENVIRONMENT
, cwd
, argc
> 0);
615 hash_algo
= hash_algo_by_name(object_format
);
616 if (hash_algo
== GIT_HASH_UNKNOWN
)
617 die(_("unknown hash algorithm '%s'"), object_format
);
620 if (init_shared_repository
!= -1)
621 set_shared_repository(init_shared_repository
);
624 * GIT_WORK_TREE makes sense only in conjunction with GIT_DIR
625 * without --bare. Catch the error early.
627 git_dir
= xstrdup_or_null(getenv(GIT_DIR_ENVIRONMENT
));
628 work_tree
= xstrdup_or_null(getenv(GIT_WORK_TREE_ENVIRONMENT
));
629 if ((!git_dir
|| is_bare_repository_cfg
== 1) && work_tree
)
630 die(_("%s (or --work-tree=<directory>) not allowed without "
631 "specifying %s (or --git-dir=<directory>)"),
632 GIT_WORK_TREE_ENVIRONMENT
,
633 GIT_DIR_ENVIRONMENT
);
636 * Set up the default .git directory contents
639 git_dir
= DEFAULT_GIT_DIR_ENVIRONMENT
;
642 * When --separate-git-dir is used inside a linked worktree, take
643 * care to ensure that the common .git/ directory is relocated, not
644 * the worktree-specific .git/worktrees/<id>/ directory.
649 struct strbuf sb
= STRBUF_INIT
;
651 p
= read_gitfile_gently(git_dir
, &err
);
652 if (p
&& get_common_dir(&sb
, p
)) {
653 struct strbuf mainwt
= STRBUF_INIT
;
655 strbuf_addbuf(&mainwt
, &sb
);
656 strbuf_strip_suffix(&mainwt
, "/.git");
657 if (chdir(mainwt
.buf
) < 0)
658 die_errno(_("cannot chdir to %s"), mainwt
.buf
);
659 strbuf_release(&mainwt
);
660 git_dir
= strbuf_detach(&sb
, NULL
);
665 if (is_bare_repository_cfg
< 0)
666 is_bare_repository_cfg
= guess_repository_type(git_dir
);
668 if (!is_bare_repository_cfg
) {
669 const char *git_dir_parent
= strrchr(git_dir
, '/');
670 if (git_dir_parent
) {
671 char *rel
= xstrndup(git_dir
, git_dir_parent
- git_dir
);
672 git_work_tree_cfg
= real_pathdup(rel
, 1);
675 if (!git_work_tree_cfg
)
676 git_work_tree_cfg
= xgetcwd();
678 set_git_work_tree(work_tree
);
680 set_git_work_tree(git_work_tree_cfg
);
681 if (access(get_git_work_tree(), X_OK
))
682 die_errno (_("Cannot access work tree '%s'"),
683 get_git_work_tree());
687 die(_("--separate-git-dir incompatible with bare repository"));
689 set_git_work_tree(work_tree
);
692 UNLEAK(real_git_dir
);
696 flags
|= INIT_DB_EXIST_OK
;
697 return init_db(git_dir
, real_git_dir
, template_dir
, hash_algo
,
698 initial_branch
, flags
);