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;
28 static const char *init_db_template_dir
;
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
)
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_db_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
);
157 static int git_init_db_config(const char *k
, const char *v
, void *cb
)
159 if (!strcmp(k
, "init.templatedir"))
160 return git_config_pathname(&init_db_template_dir
, k
, v
);
162 if (starts_with(k
, "core."))
163 return platform_core_config(k
, v
, cb
);
169 * If the git_dir is not directly inside the working tree, then git will not
170 * find it by default, and we need to set the worktree explicitly.
172 static int needs_work_tree_config(const char *git_dir
, const char *work_tree
)
174 if (!strcmp(work_tree
, "/") && !strcmp(git_dir
, "/.git"))
176 if (skip_prefix(git_dir
, work_tree
, &git_dir
) &&
177 !strcmp(git_dir
, "/.git"))
182 void initialize_repository_version(int hash_algo
, int reinit
)
184 char repo_version_string
[10];
185 int repo_version
= GIT_REPO_VERSION
;
187 if (hash_algo
!= GIT_HASH_SHA1
)
188 repo_version
= GIT_REPO_VERSION_READ
;
190 /* This forces creation of new config file */
191 xsnprintf(repo_version_string
, sizeof(repo_version_string
),
193 git_config_set("core.repositoryformatversion", repo_version_string
);
195 if (hash_algo
!= GIT_HASH_SHA1
)
196 git_config_set("extensions.objectformat",
197 hash_algos
[hash_algo
].name
);
199 git_config_set_gently("extensions.objectformat", NULL
);
202 static int create_default_files(const char *template_path
,
203 const char *original_git_dir
,
204 const char *initial_branch
,
205 const struct repository_format
*fmt
)
208 struct strbuf buf
= STRBUF_INIT
;
213 struct strbuf err
= STRBUF_INIT
;
215 /* Just look for `init.templatedir` */
216 init_db_template_dir
= NULL
; /* re-set in case it was set before */
217 git_config(git_init_db_config
, NULL
);
220 * First copy the templates -- we might have the default
221 * config file there, in which case we would want to read
222 * from it after installing.
224 * Before reading that config, we also need to clear out any cached
225 * values (since we've just potentially changed what's available on
228 copy_templates(template_path
);
230 reset_shared_repository();
231 git_config(git_default_config
, NULL
);
234 * We must make sure command-line options continue to override any
235 * values we might have just re-read from the config.
237 is_bare_repository_cfg
= init_is_bare_repository
;
238 if (init_shared_repository
!= -1)
239 set_shared_repository(init_shared_repository
);
242 * We would have created the above under user's umask -- under
243 * shared-repository settings, we would need to fix them up.
245 if (get_shared_repository()) {
246 adjust_shared_perm(get_git_dir());
250 * We need to create a "refs" dir in any case so that older
251 * versions of git can tell that this is a repository.
253 safe_create_dir(git_path("refs"), 1);
254 adjust_shared_perm(git_path("refs"));
256 if (refs_init_db(&err
))
257 die("failed to set up refs db: %s", err
.buf
);
260 * Point the HEAD symref to the initial branch with if HEAD does
263 path
= git_path_buf(&buf
, "HEAD");
264 reinit
= (!access(path
, R_OK
)
265 || readlink(path
, junk
, sizeof(junk
)-1) != -1);
270 initial_branch
= git_default_branch_name();
272 ref
= xstrfmt("refs/heads/%s", initial_branch
);
273 if (check_refname_format(ref
, 0) < 0)
274 die(_("invalid initial branch name: '%s'"),
277 if (create_symref("HEAD", ref
, NULL
) < 0)
282 initialize_repository_version(fmt
->hash_algo
, 0);
284 /* Check filemode trustability */
285 path
= git_path_buf(&buf
, "config");
286 filemode
= TEST_FILEMODE
;
287 if (TEST_FILEMODE
&& !lstat(path
, &st1
)) {
289 filemode
= (!chmod(path
, st1
.st_mode
^ S_IXUSR
) &&
290 !lstat(path
, &st2
) &&
291 st1
.st_mode
!= st2
.st_mode
&&
292 !chmod(path
, st1
.st_mode
));
293 if (filemode
&& !reinit
&& (st1
.st_mode
& S_IXUSR
))
296 git_config_set("core.filemode", filemode
? "true" : "false");
298 if (is_bare_repository())
299 git_config_set("core.bare", "true");
301 const char *work_tree
= get_git_work_tree();
302 git_config_set("core.bare", "false");
303 /* allow template config file to override the default */
304 if (log_all_ref_updates
== LOG_REFS_UNSET
)
305 git_config_set("core.logallrefupdates", "true");
306 if (needs_work_tree_config(original_git_dir
, work_tree
))
307 git_config_set("core.worktree", work_tree
);
311 /* Check if symlink is supported in the work tree */
312 path
= git_path_buf(&buf
, "tXXXXXX");
313 if (!close(xmkstemp(path
)) &&
315 !symlink("testing", path
) &&
316 !lstat(path
, &st1
) &&
317 S_ISLNK(st1
.st_mode
))
318 unlink(path
); /* good */
320 git_config_set("core.symlinks", "false");
322 /* Check if the filesystem is case-insensitive */
323 path
= git_path_buf(&buf
, "CoNfIg");
324 if (!access(path
, F_OK
))
325 git_config_set("core.ignorecase", "true");
326 probe_utf8_pathname_composition();
329 strbuf_release(&buf
);
333 static void create_object_directory(void)
335 struct strbuf path
= STRBUF_INIT
;
338 strbuf_addstr(&path
, get_object_directory());
341 safe_create_dir(path
.buf
, 1);
343 strbuf_setlen(&path
, baselen
);
344 strbuf_addstr(&path
, "/pack");
345 safe_create_dir(path
.buf
, 1);
347 strbuf_setlen(&path
, baselen
);
348 strbuf_addstr(&path
, "/info");
349 safe_create_dir(path
.buf
, 1);
351 strbuf_release(&path
);
354 static void separate_git_dir(const char *git_dir
, const char *git_link
)
358 if (!stat(git_link
, &st
)) {
361 if (S_ISREG(st
.st_mode
))
362 src
= read_gitfile(git_link
);
363 else if (S_ISDIR(st
.st_mode
))
366 die(_("unable to handle file type %d"), (int)st
.st_mode
);
368 if (rename(src
, git_dir
))
369 die_errno(_("unable to move %s to %s"), src
, git_dir
);
370 repair_worktrees(NULL
, NULL
);
373 write_file(git_link
, "gitdir: %s", git_dir
);
376 static void validate_hash_algorithm(struct repository_format
*repo_fmt
, int hash
)
378 const char *env
= getenv(GIT_DEFAULT_HASH_ENVIRONMENT
);
380 * If we already have an initialized repo, don't allow the user to
381 * specify a different algorithm, as that could cause corruption.
382 * Otherwise, if the user has specified one on the command line, use it.
384 if (repo_fmt
->version
>= 0 && hash
!= GIT_HASH_UNKNOWN
&& hash
!= repo_fmt
->hash_algo
)
385 die(_("attempt to reinitialize repository with different hash"));
386 else if (hash
!= GIT_HASH_UNKNOWN
)
387 repo_fmt
->hash_algo
= hash
;
389 int env_algo
= hash_algo_by_name(env
);
390 if (env_algo
== GIT_HASH_UNKNOWN
)
391 die(_("unknown hash algorithm '%s'"), env
);
392 repo_fmt
->hash_algo
= env_algo
;
396 int init_db(const char *git_dir
, const char *real_git_dir
,
397 const char *template_dir
, int hash
, const char *initial_branch
,
401 int exist_ok
= flags
& INIT_DB_EXIST_OK
;
402 char *original_git_dir
= real_pathdup(git_dir
, 1);
403 struct repository_format repo_fmt
= REPOSITORY_FORMAT_INIT
;
408 if (!exist_ok
&& !stat(git_dir
, &st
))
409 die(_("%s already exists"), git_dir
);
411 if (!exist_ok
&& !stat(real_git_dir
, &st
))
412 die(_("%s already exists"), real_git_dir
);
414 set_git_dir(real_git_dir
, 1);
415 git_dir
= get_git_dir();
416 separate_git_dir(git_dir
, original_git_dir
);
419 set_git_dir(git_dir
, 1);
420 git_dir
= get_git_dir();
422 startup_info
->have_repository
= 1;
424 /* Just look for `core.hidedotfiles` */
425 git_config(git_init_db_config
, NULL
);
427 safe_create_dir(git_dir
, 0);
429 init_is_bare_repository
= is_bare_repository();
431 /* Check to see if the repository version is right.
432 * Note that a newly created repository does not have
433 * config file, so this will not fail. What we are catching
434 * is an attempt to reinitialize new repository with an old tool.
436 check_repository_format(&repo_fmt
);
438 validate_hash_algorithm(&repo_fmt
, hash
);
440 reinit
= create_default_files(template_dir
, original_git_dir
,
441 initial_branch
, &repo_fmt
);
442 if (reinit
&& initial_branch
)
443 warning(_("re-init: ignored --initial-branch=%s"),
446 create_object_directory();
448 if (get_shared_repository()) {
450 /* We do not spell "group" and such, so that
451 * the configuration can be read by older version
452 * of git. Note, we use octal numbers for new share modes,
453 * and compatibility values for PERM_GROUP and
456 if (get_shared_repository() < 0)
457 /* force to the mode value */
458 xsnprintf(buf
, sizeof(buf
), "0%o", -get_shared_repository());
459 else if (get_shared_repository() == PERM_GROUP
)
460 xsnprintf(buf
, sizeof(buf
), "%d", OLD_PERM_GROUP
);
461 else if (get_shared_repository() == PERM_EVERYBODY
)
462 xsnprintf(buf
, sizeof(buf
), "%d", OLD_PERM_EVERYBODY
);
464 BUG("invalid value for shared_repository");
465 git_config_set("core.sharedrepository", buf
);
466 git_config_set("receive.denyNonFastforwards", "true");
469 if (!(flags
& INIT_DB_QUIET
)) {
470 int len
= strlen(git_dir
);
473 printf(get_shared_repository()
474 ? _("Reinitialized existing shared Git repository in %s%s\n")
475 : _("Reinitialized existing Git repository in %s%s\n"),
476 git_dir
, len
&& git_dir
[len
-1] != '/' ? "/" : "");
478 printf(get_shared_repository()
479 ? _("Initialized empty shared Git repository in %s%s\n")
480 : _("Initialized empty Git repository in %s%s\n"),
481 git_dir
, len
&& git_dir
[len
-1] != '/' ? "/" : "");
484 free(original_git_dir
);
488 static int guess_repository_type(const char *git_dir
)
495 * "GIT_DIR=. git init" is always bare.
496 * "GIT_DIR=`pwd` git init" too.
498 if (!strcmp(".", git_dir
))
501 cwd_is_git_dir
= !strcmp(git_dir
, cwd
);
506 * "GIT_DIR=.git or GIT_DIR=something/.git is usually not.
508 if (!strcmp(git_dir
, ".git"))
510 slash
= strrchr(git_dir
, '/');
511 if (slash
&& !strcmp(slash
, "/.git"))
515 * Otherwise it is often bare. At this point
516 * we are just guessing.
521 static int shared_callback(const struct option
*opt
, const char *arg
, int unset
)
523 BUG_ON_OPT_NEG(unset
);
524 *((int *) opt
->value
) = (arg
) ? git_config_perm("arg", arg
) : PERM_GROUP
;
528 static const char *const init_db_usage
[] = {
529 N_("git init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]] [<directory>]"),
534 * If you want to, you can share the DB area with any number of branches.
535 * That has advantages: you can save space by sharing all the SHA1 objects.
536 * On the other hand, it might just make lookup slower and messier. You
537 * be the judge. The default case is to have one DB per managed directory.
539 int cmd_init_db(int argc
, const char **argv
, const char *prefix
)
542 const char *real_git_dir
= NULL
;
543 const char *work_tree
;
544 const char *template_dir
= NULL
;
545 unsigned int flags
= 0;
546 const char *object_format
= NULL
;
547 const char *initial_branch
= NULL
;
548 int hash_algo
= GIT_HASH_UNKNOWN
;
549 const struct option init_db_options
[] = {
550 OPT_STRING(0, "template", &template_dir
, N_("template-directory"),
551 N_("directory from which templates will be used")),
552 OPT_SET_INT(0, "bare", &is_bare_repository_cfg
,
553 N_("create a bare repository"), 1),
554 { OPTION_CALLBACK
, 0, "shared", &init_shared_repository
,
556 N_("specify that the git repository is to be shared amongst several users"),
557 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
, shared_callback
, 0},
558 OPT_BIT('q', "quiet", &flags
, N_("be quiet"), INIT_DB_QUIET
),
559 OPT_STRING(0, "separate-git-dir", &real_git_dir
, N_("gitdir"),
560 N_("separate git dir from working tree")),
561 OPT_STRING('b', "initial-branch", &initial_branch
, N_("name"),
562 N_("override the name of the initial branch")),
563 OPT_STRING(0, "object-format", &object_format
, N_("hash"),
564 N_("specify the hash algorithm to use")),
568 argc
= parse_options(argc
, argv
, prefix
, init_db_options
, init_db_usage
, 0);
570 if (real_git_dir
&& is_bare_repository_cfg
== 1)
571 die(_("--separate-git-dir and --bare are mutually exclusive"));
573 if (real_git_dir
&& !is_absolute_path(real_git_dir
))
574 real_git_dir
= real_pathdup(real_git_dir
, 1);
576 if (template_dir
&& *template_dir
&& !is_absolute_path(template_dir
))
577 template_dir
= absolute_pathdup(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
);