2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
10 #include "parse-options.h"
12 #ifndef DEFAULT_GIT_TEMPLATE_DIR
13 #define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates"
16 #ifdef NO_TRUSTABLE_FILEMODE
17 #define TEST_FILEMODE 0
19 #define TEST_FILEMODE 1
22 static int init_is_bare_repository
= 0;
23 static int init_shared_repository
= -1;
24 static const char *init_db_template_dir
;
25 static const char *git_link
;
27 static void safe_create_dir(const char *dir
, int share
)
29 if (mkdir(dir
, 0777) < 0) {
30 if (errno
!= EEXIST
) {
35 else if (share
&& adjust_shared_perm(dir
))
36 die(_("Could not make %s writable by group"), dir
);
39 static void copy_templates_1(char *path
, int baselen
,
40 char *template, int template_baselen
,
45 /* Note: if ".git/hooks" file exists in the repository being
46 * re-initialized, /etc/core-git/templates/hooks/update would
47 * cause "git init" to fail here. I think this is sane but
48 * it means that the set of templates we ship by default, along
49 * with the way the namespace under .git/ is organized, should
50 * be really carefully chosen.
52 safe_create_dir(path
, 1);
53 while ((de
= readdir(dir
)) != NULL
) {
54 struct stat st_git
, st_template
;
58 if (de
->d_name
[0] == '.')
60 namelen
= strlen(de
->d_name
);
61 if ((PATH_MAX
<= baselen
+ namelen
) ||
62 (PATH_MAX
<= template_baselen
+ namelen
))
63 die(_("insanely long template name %s"), de
->d_name
);
64 memcpy(path
+ baselen
, de
->d_name
, namelen
+1);
65 memcpy(template + template_baselen
, de
->d_name
, namelen
+1);
66 if (lstat(path
, &st_git
)) {
68 die_errno(_("cannot stat '%s'"), path
);
73 if (lstat(template, &st_template
))
74 die_errno(_("cannot stat template '%s'"), template);
76 if (S_ISDIR(st_template
.st_mode
)) {
77 DIR *subdir
= opendir(template);
78 int baselen_sub
= baselen
+ namelen
;
79 int template_baselen_sub
= template_baselen
+ namelen
;
81 die_errno(_("cannot opendir '%s'"), template);
83 template[template_baselen_sub
++] = '/';
85 template[template_baselen_sub
] = 0;
86 copy_templates_1(path
, baselen_sub
,
87 template, template_baselen_sub
,
93 else if (S_ISLNK(st_template
.st_mode
)) {
96 len
= readlink(template, lnk
, sizeof(lnk
));
98 die_errno(_("cannot readlink '%s'"), template);
99 if (sizeof(lnk
) <= len
)
100 die(_("insanely long symlink %s"), template);
102 if (symlink(lnk
, path
))
103 die_errno(_("cannot symlink '%s' '%s'"), lnk
, path
);
105 else if (S_ISREG(st_template
.st_mode
)) {
106 if (copy_file(path
, template, st_template
.st_mode
))
107 die_errno(_("cannot copy '%s' to '%s'"), template,
111 error(_("ignoring template %s"), template);
115 static void copy_templates(const char *template_dir
)
118 char template_path
[PATH_MAX
];
121 const char *git_dir
= get_git_dir();
122 int len
= strlen(git_dir
);
123 char *to_free
= NULL
;
126 template_dir
= getenv(TEMPLATE_DIR_ENVIRONMENT
);
128 template_dir
= init_db_template_dir
;
130 template_dir
= to_free
= system_path(DEFAULT_GIT_TEMPLATE_DIR
);
131 if (!template_dir
[0]) {
135 template_len
= strlen(template_dir
);
136 if (PATH_MAX
<= (template_len
+strlen("/config")))
137 die(_("insanely long template path %s"), template_dir
);
138 strcpy(template_path
, template_dir
);
139 if (template_path
[template_len
-1] != '/') {
140 template_path
[template_len
++] = '/';
141 template_path
[template_len
] = 0;
143 dir
= opendir(template_path
);
145 warning(_("templates not found %s"), template_dir
);
149 /* Make sure that template is from the correct vintage */
150 strcpy(template_path
+ template_len
, "config");
151 repository_format_version
= 0;
152 git_config_from_file(check_repository_format_version
,
153 template_path
, NULL
);
154 template_path
[template_len
] = 0;
156 if (repository_format_version
&&
157 repository_format_version
!= GIT_REPO_VERSION
) {
158 warning(_("not copying templates of "
159 "a wrong format version %d from '%s'"),
160 repository_format_version
,
162 goto close_free_return
;
165 memcpy(path
, git_dir
, len
);
166 if (len
&& path
[len
- 1] != '/')
169 copy_templates_1(path
, len
,
170 template_path
, template_len
,
178 static int git_init_db_config(const char *k
, const char *v
, void *cb
)
180 if (!strcmp(k
, "init.templatedir"))
181 return git_config_pathname(&init_db_template_dir
, k
, v
);
187 * If the git_dir is not directly inside the working tree, then git will not
188 * find it by default, and we need to set the worktree explicitly.
190 static int needs_work_tree_config(const char *git_dir
, const char *work_tree
)
192 if (!strcmp(work_tree
, "/") && !strcmp(git_dir
, "/.git"))
194 if (skip_prefix(git_dir
, work_tree
, &git_dir
) &&
195 !strcmp(git_dir
, "/.git"))
200 static int create_default_files(const char *template_path
)
202 const char *git_dir
= get_git_dir();
203 unsigned len
= strlen(git_dir
);
204 static char path
[PATH_MAX
];
206 char repo_version_string
[10];
211 if (len
> sizeof(path
)-50)
212 die(_("insane git directory %s"), git_dir
);
213 memcpy(path
, git_dir
, len
);
215 if (len
&& path
[len
-1] != '/')
219 * Create .git/refs/{heads,tags}
221 safe_create_dir(git_path("refs"), 1);
222 safe_create_dir(git_path("refs/heads"), 1);
223 safe_create_dir(git_path("refs/tags"), 1);
225 /* Just look for `init.templatedir` */
226 git_config(git_init_db_config
, NULL
);
228 /* First copy the templates -- we might have the default
229 * config file there, in which case we would want to read
230 * from it after installing.
232 copy_templates(template_path
);
234 git_config(git_default_config
, NULL
);
235 is_bare_repository_cfg
= init_is_bare_repository
;
237 /* reading existing config may have overwrote it */
238 if (init_shared_repository
!= -1)
239 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 (shared_repository
) {
246 adjust_shared_perm(get_git_dir());
247 adjust_shared_perm(git_path("refs"));
248 adjust_shared_perm(git_path("refs/heads"));
249 adjust_shared_perm(git_path("refs/tags"));
253 * Create the default symlink from ".git/HEAD" to the "master"
254 * branch, if it does not exist yet.
256 strcpy(path
+ len
, "HEAD");
257 reinit
= (!access(path
, R_OK
)
258 || readlink(path
, junk
, sizeof(junk
)-1) != -1);
260 if (create_symref("HEAD", "refs/heads/master", NULL
) < 0)
264 /* This forces creation of new config file */
265 xsnprintf(repo_version_string
, sizeof(repo_version_string
),
266 "%d", GIT_REPO_VERSION
);
267 git_config_set("core.repositoryformatversion", repo_version_string
);
270 strcpy(path
+ len
, "config");
272 /* Check filemode trustability */
273 filemode
= TEST_FILEMODE
;
274 if (TEST_FILEMODE
&& !lstat(path
, &st1
)) {
276 filemode
= (!chmod(path
, st1
.st_mode
^ S_IXUSR
) &&
277 !lstat(path
, &st2
) &&
278 st1
.st_mode
!= st2
.st_mode
&&
279 !chmod(path
, st1
.st_mode
));
280 if (filemode
&& !reinit
&& (st1
.st_mode
& S_IXUSR
))
283 git_config_set("core.filemode", filemode
? "true" : "false");
285 if (is_bare_repository())
286 git_config_set("core.bare", "true");
288 const char *work_tree
= get_git_work_tree();
289 git_config_set("core.bare", "false");
290 /* allow template config file to override the default */
291 if (log_all_ref_updates
== -1)
292 git_config_set("core.logallrefupdates", "true");
293 if (needs_work_tree_config(git_dir
, work_tree
))
294 git_config_set("core.worktree", work_tree
);
298 /* Check if symlink is supported in the work tree */
300 strcpy(path
+ len
, "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 */
312 strcpy(path
+ len
, "CoNfIg");
313 if (!access(path
, F_OK
))
314 git_config_set("core.ignorecase", "true");
315 probe_utf8_pathname_composition();
321 static void create_object_directory(void)
323 const char *object_directory
= get_object_directory();
324 int len
= strlen(object_directory
);
325 char *path
= xmalloc(len
+ 40);
327 memcpy(path
, object_directory
, len
);
329 safe_create_dir(object_directory
, 1);
330 strcpy(path
+len
, "/pack");
331 safe_create_dir(path
, 1);
332 strcpy(path
+len
, "/info");
333 safe_create_dir(path
, 1);
338 int set_git_dir_init(const char *git_dir
, const char *real_git_dir
,
344 if (!exist_ok
&& !stat(git_dir
, &st
))
345 die(_("%s already exists"), git_dir
);
347 if (!exist_ok
&& !stat(real_git_dir
, &st
))
348 die(_("%s already exists"), real_git_dir
);
351 * make sure symlinks are resolved because we'll be
352 * moving the target repo later on in separate_git_dir()
354 git_link
= xstrdup(real_path(git_dir
));
355 set_git_dir(real_path(real_git_dir
));
358 set_git_dir(real_path(git_dir
));
364 static void separate_git_dir(const char *git_dir
)
368 if (!stat(git_link
, &st
)) {
371 if (S_ISREG(st
.st_mode
))
372 src
= read_gitfile(git_link
);
373 else if (S_ISDIR(st
.st_mode
))
376 die(_("unable to handle file type %d"), (int)st
.st_mode
);
378 if (rename(src
, git_dir
))
379 die_errno(_("unable to move %s to %s"), src
, git_dir
);
382 write_file(git_link
, "gitdir: %s", git_dir
);
385 int init_db(const char *template_dir
, unsigned int flags
)
388 const char *git_dir
= get_git_dir();
391 separate_git_dir(git_dir
);
393 safe_create_dir(git_dir
, 0);
395 init_is_bare_repository
= is_bare_repository();
397 /* Check to see if the repository version is right.
398 * Note that a newly created repository does not have
399 * config file, so this will not fail. What we are catching
400 * is an attempt to reinitialize new repository with an old tool.
402 check_repository_format();
404 reinit
= create_default_files(template_dir
);
406 create_object_directory();
408 if (shared_repository
) {
410 /* We do not spell "group" and such, so that
411 * the configuration can be read by older version
412 * of git. Note, we use octal numbers for new share modes,
413 * and compatibility values for PERM_GROUP and
416 if (shared_repository
< 0)
417 /* force to the mode value */
418 xsnprintf(buf
, sizeof(buf
), "0%o", -shared_repository
);
419 else if (shared_repository
== PERM_GROUP
)
420 xsnprintf(buf
, sizeof(buf
), "%d", OLD_PERM_GROUP
);
421 else if (shared_repository
== PERM_EVERYBODY
)
422 xsnprintf(buf
, sizeof(buf
), "%d", OLD_PERM_EVERYBODY
);
424 die("BUG: invalid value for shared_repository");
425 git_config_set("core.sharedrepository", buf
);
426 git_config_set("receive.denyNonFastforwards", "true");
429 if (!(flags
& INIT_DB_QUIET
)) {
430 int len
= strlen(git_dir
);
432 /* TRANSLATORS: The first '%s' is either "Reinitialized
433 existing" or "Initialized empty", the second " shared" or
434 "", and the last '%s%s' is the verbatim directory name. */
435 printf(_("%s%s Git repository in %s%s\n"),
436 reinit
? _("Reinitialized existing") : _("Initialized empty"),
437 shared_repository
? _(" shared") : "",
438 git_dir
, len
&& git_dir
[len
-1] != '/' ? "/" : "");
444 static int guess_repository_type(const char *git_dir
)
451 * "GIT_DIR=. git init" is always bare.
452 * "GIT_DIR=`pwd` git init" too.
454 if (!strcmp(".", git_dir
))
457 cwd_is_git_dir
= !strcmp(git_dir
, cwd
);
462 * "GIT_DIR=.git or GIT_DIR=something/.git is usually not.
464 if (!strcmp(git_dir
, ".git"))
466 slash
= strrchr(git_dir
, '/');
467 if (slash
&& !strcmp(slash
, "/.git"))
471 * Otherwise it is often bare. At this point
472 * we are just guessing.
477 static int shared_callback(const struct option
*opt
, const char *arg
, int unset
)
479 *((int *) opt
->value
) = (arg
) ? git_config_perm("arg", arg
) : PERM_GROUP
;
483 static const char *const init_db_usage
[] = {
484 N_("git init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]] [<directory>]"),
489 * If you want to, you can share the DB area with any number of branches.
490 * That has advantages: you can save space by sharing all the SHA1 objects.
491 * On the other hand, it might just make lookup slower and messier. You
492 * be the judge. The default case is to have one DB per managed directory.
494 int cmd_init_db(int argc
, const char **argv
, const char *prefix
)
497 const char *real_git_dir
= NULL
;
498 const char *work_tree
;
499 const char *template_dir
= NULL
;
500 unsigned int flags
= 0;
501 const struct option init_db_options
[] = {
502 OPT_STRING(0, "template", &template_dir
, N_("template-directory"),
503 N_("directory from which templates will be used")),
504 OPT_SET_INT(0, "bare", &is_bare_repository_cfg
,
505 N_("create a bare repository"), 1),
506 { OPTION_CALLBACK
, 0, "shared", &init_shared_repository
,
508 N_("specify that the git repository is to be shared amongst several users"),
509 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
, shared_callback
, 0},
510 OPT_BIT('q', "quiet", &flags
, N_("be quiet"), INIT_DB_QUIET
),
511 OPT_STRING(0, "separate-git-dir", &real_git_dir
, N_("gitdir"),
512 N_("separate git dir from working tree")),
516 argc
= parse_options(argc
, argv
, prefix
, init_db_options
, init_db_usage
, 0);
518 if (real_git_dir
&& !is_absolute_path(real_git_dir
))
519 real_git_dir
= xstrdup(real_path(real_git_dir
));
524 if (chdir(argv
[0]) < 0) {
528 * At this point we haven't read any configuration,
529 * and we know shared_repository should always be 0;
530 * but just in case we play safe.
532 saved
= shared_repository
;
533 shared_repository
= 0;
534 switch (safe_create_leading_directories_const(argv
[0])) {
542 die_errno(_("cannot mkdir %s"), argv
[0]);
545 shared_repository
= saved
;
546 if (mkdir(argv
[0], 0777) < 0)
547 die_errno(_("cannot mkdir %s"), argv
[0]);
551 die_errno(_("cannot chdir to %s"), argv
[0]);
553 } else if (0 < argc
) {
554 usage(init_db_usage
[0]);
556 if (is_bare_repository_cfg
== 1) {
557 char *cwd
= xgetcwd();
558 setenv(GIT_DIR_ENVIRONMENT
, cwd
, argc
> 0);
562 if (init_shared_repository
!= -1)
563 shared_repository
= init_shared_repository
;
566 * GIT_WORK_TREE makes sense only in conjunction with GIT_DIR
567 * without --bare. Catch the error early.
569 git_dir
= getenv(GIT_DIR_ENVIRONMENT
);
570 work_tree
= getenv(GIT_WORK_TREE_ENVIRONMENT
);
571 if ((!git_dir
|| is_bare_repository_cfg
== 1) && work_tree
)
572 die(_("%s (or --work-tree=<directory>) not allowed without "
573 "specifying %s (or --git-dir=<directory>)"),
574 GIT_WORK_TREE_ENVIRONMENT
,
575 GIT_DIR_ENVIRONMENT
);
578 * Set up the default .git directory contents
581 git_dir
= DEFAULT_GIT_DIR_ENVIRONMENT
;
583 if (is_bare_repository_cfg
< 0)
584 is_bare_repository_cfg
= guess_repository_type(git_dir
);
586 if (!is_bare_repository_cfg
) {
587 const char *git_dir_parent
= strrchr(git_dir
, '/');
588 if (git_dir_parent
) {
589 char *rel
= xstrndup(git_dir
, git_dir_parent
- git_dir
);
590 git_work_tree_cfg
= xstrdup(real_path(rel
));
593 if (!git_work_tree_cfg
)
594 git_work_tree_cfg
= xgetcwd();
596 set_git_work_tree(work_tree
);
598 set_git_work_tree(git_work_tree_cfg
);
599 if (access(get_git_work_tree(), X_OK
))
600 die_errno (_("Cannot access work tree '%s'"),
601 get_git_work_tree());
605 set_git_work_tree(work_tree
);
608 set_git_dir_init(git_dir
, real_git_dir
, 1);
610 return init_db(template_dir
, flags
);