2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
9 #include "parse-options.h"
11 #ifndef DEFAULT_GIT_TEMPLATE_DIR
12 #define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates"
15 #ifdef NO_TRUSTABLE_FILEMODE
16 #define TEST_FILEMODE 0
18 #define TEST_FILEMODE 1
21 static int init_is_bare_repository
= 0;
22 static int init_shared_repository
= -1;
23 static const char *init_db_template_dir
;
24 static const char *git_link
;
26 static void safe_create_dir(const char *dir
, int share
)
28 if (mkdir(dir
, 0777) < 0) {
29 if (errno
!= EEXIST
) {
34 else if (share
&& adjust_shared_perm(dir
))
35 die(_("Could not make %s writable by group"), dir
);
38 static void copy_templates_1(char *path
, int baselen
,
39 char *template, int template_baselen
,
44 /* Note: if ".git/hooks" file exists in the repository being
45 * re-initialized, /etc/core-git/templates/hooks/update would
46 * cause "git init" to fail here. I think this is sane but
47 * it means that the set of templates we ship by default, along
48 * with the way the namespace under .git/ is organized, should
49 * be really carefully chosen.
51 safe_create_dir(path
, 1);
52 while ((de
= readdir(dir
)) != NULL
) {
53 struct stat st_git
, st_template
;
57 if (de
->d_name
[0] == '.')
59 namelen
= strlen(de
->d_name
);
60 if ((PATH_MAX
<= baselen
+ namelen
) ||
61 (PATH_MAX
<= template_baselen
+ namelen
))
62 die(_("insanely long template name %s"), de
->d_name
);
63 memcpy(path
+ baselen
, de
->d_name
, namelen
+1);
64 memcpy(template + template_baselen
, de
->d_name
, namelen
+1);
65 if (lstat(path
, &st_git
)) {
67 die_errno(_("cannot stat '%s'"), path
);
72 if (lstat(template, &st_template
))
73 die_errno(_("cannot stat template '%s'"), template);
75 if (S_ISDIR(st_template
.st_mode
)) {
76 DIR *subdir
= opendir(template);
77 int baselen_sub
= baselen
+ namelen
;
78 int template_baselen_sub
= template_baselen
+ namelen
;
80 die_errno(_("cannot opendir '%s'"), template);
82 template[template_baselen_sub
++] = '/';
84 template[template_baselen_sub
] = 0;
85 copy_templates_1(path
, baselen_sub
,
86 template, template_baselen_sub
,
92 else if (S_ISLNK(st_template
.st_mode
)) {
95 len
= readlink(template, lnk
, sizeof(lnk
));
97 die_errno(_("cannot readlink '%s'"), template);
98 if (sizeof(lnk
) <= len
)
99 die(_("insanely long symlink %s"), template);
101 if (symlink(lnk
, path
))
102 die_errno(_("cannot symlink '%s' '%s'"), lnk
, path
);
104 else if (S_ISREG(st_template
.st_mode
)) {
105 if (copy_file(path
, template, st_template
.st_mode
))
106 die_errno(_("cannot copy '%s' to '%s'"), template,
110 error(_("ignoring template %s"), template);
114 static void copy_templates(const char *template_dir
)
117 char template_path
[PATH_MAX
];
120 const char *git_dir
= get_git_dir();
121 int len
= strlen(git_dir
);
124 template_dir
= getenv(TEMPLATE_DIR_ENVIRONMENT
);
126 template_dir
= init_db_template_dir
;
128 template_dir
= system_path(DEFAULT_GIT_TEMPLATE_DIR
);
129 if (!template_dir
[0])
131 template_len
= strlen(template_dir
);
132 if (PATH_MAX
<= (template_len
+strlen("/config")))
133 die(_("insanely long template path %s"), template_dir
);
134 strcpy(template_path
, template_dir
);
135 if (template_path
[template_len
-1] != '/') {
136 template_path
[template_len
++] = '/';
137 template_path
[template_len
] = 0;
139 dir
= opendir(template_path
);
141 warning(_("templates not found %s"), template_dir
);
145 /* Make sure that template is from the correct vintage */
146 strcpy(template_path
+ template_len
, "config");
147 repository_format_version
= 0;
148 git_config_from_file(check_repository_format_version
,
149 template_path
, NULL
);
150 template_path
[template_len
] = 0;
152 if (repository_format_version
&&
153 repository_format_version
!= GIT_REPO_VERSION
) {
154 warning(_("not copying templates of "
155 "a wrong format version %d from '%s'"),
156 repository_format_version
,
162 memcpy(path
, git_dir
, len
);
163 if (len
&& path
[len
- 1] != '/')
166 copy_templates_1(path
, len
,
167 template_path
, template_len
,
172 static int git_init_db_config(const char *k
, const char *v
, void *cb
)
174 if (!strcmp(k
, "init.templatedir"))
175 return git_config_pathname(&init_db_template_dir
, k
, v
);
180 static int create_default_files(const char *template_path
)
182 const char *git_dir
= get_git_dir();
183 unsigned len
= strlen(git_dir
);
184 static char path
[PATH_MAX
];
186 char repo_version_string
[10];
191 if (len
> sizeof(path
)-50)
192 die(_("insane git directory %s"), git_dir
);
193 memcpy(path
, git_dir
, len
);
195 if (len
&& path
[len
-1] != '/')
199 * Create .git/refs/{heads,tags}
201 safe_create_dir(git_path("refs"), 1);
202 safe_create_dir(git_path("refs/heads"), 1);
203 safe_create_dir(git_path("refs/tags"), 1);
205 /* Just look for `init.templatedir` */
206 git_config(git_init_db_config
, NULL
);
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 copy_templates(template_path
);
214 git_config(git_default_config
, NULL
);
215 is_bare_repository_cfg
= init_is_bare_repository
;
217 /* reading existing config may have overwrote it */
218 if (init_shared_repository
!= -1)
219 shared_repository
= init_shared_repository
;
222 * We would have created the above under user's umask -- under
223 * shared-repository settings, we would need to fix them up.
225 if (shared_repository
) {
226 adjust_shared_perm(get_git_dir());
227 adjust_shared_perm(git_path("refs"));
228 adjust_shared_perm(git_path("refs/heads"));
229 adjust_shared_perm(git_path("refs/tags"));
233 * Create the default symlink from ".git/HEAD" to the "master"
234 * branch, if it does not exist yet.
236 strcpy(path
+ len
, "HEAD");
237 reinit
= (!access(path
, R_OK
)
238 || readlink(path
, junk
, sizeof(junk
)-1) != -1);
240 if (create_symref("HEAD", "refs/heads/master", NULL
) < 0)
244 /* This forces creation of new config file */
245 sprintf(repo_version_string
, "%d", GIT_REPO_VERSION
);
246 git_config_set("core.repositoryformatversion", repo_version_string
);
249 strcpy(path
+ len
, "config");
251 /* Check filemode trustability */
252 filemode
= TEST_FILEMODE
;
253 if (TEST_FILEMODE
&& !lstat(path
, &st1
)) {
255 filemode
= (!chmod(path
, st1
.st_mode
^ S_IXUSR
) &&
256 !lstat(path
, &st2
) &&
257 st1
.st_mode
!= st2
.st_mode
&&
258 !chmod(path
, st1
.st_mode
));
260 git_config_set("core.filemode", filemode
? "true" : "false");
262 if (is_bare_repository())
263 git_config_set("core.bare", "true");
265 const char *work_tree
= get_git_work_tree();
266 git_config_set("core.bare", "false");
267 /* allow template config file to override the default */
268 if (log_all_ref_updates
== -1)
269 git_config_set("core.logallrefupdates", "true");
270 if (!starts_with(git_dir
, work_tree
) ||
271 strcmp(git_dir
+ strlen(work_tree
), "/.git")) {
272 git_config_set("core.worktree", work_tree
);
277 /* Check if symlink is supported in the work tree */
279 strcpy(path
+ len
, "tXXXXXX");
280 if (!close(xmkstemp(path
)) &&
282 !symlink("testing", path
) &&
283 !lstat(path
, &st1
) &&
284 S_ISLNK(st1
.st_mode
))
285 unlink(path
); /* good */
287 git_config_set("core.symlinks", "false");
289 /* Check if the filesystem is case-insensitive */
291 strcpy(path
+ len
, "CoNfIg");
292 if (!access(path
, F_OK
))
293 git_config_set("core.ignorecase", "true");
294 probe_utf8_pathname_composition(path
, len
);
300 static void create_object_directory(void)
302 const char *object_directory
= get_object_directory();
303 int len
= strlen(object_directory
);
304 char *path
= xmalloc(len
+ 40);
306 memcpy(path
, object_directory
, len
);
308 safe_create_dir(object_directory
, 1);
309 strcpy(path
+len
, "/pack");
310 safe_create_dir(path
, 1);
311 strcpy(path
+len
, "/info");
312 safe_create_dir(path
, 1);
317 int set_git_dir_init(const char *git_dir
, const char *real_git_dir
,
323 if (!exist_ok
&& !stat(git_dir
, &st
))
324 die(_("%s already exists"), git_dir
);
326 if (!exist_ok
&& !stat(real_git_dir
, &st
))
327 die(_("%s already exists"), real_git_dir
);
330 * make sure symlinks are resolved because we'll be
331 * moving the target repo later on in separate_git_dir()
333 git_link
= xstrdup(real_path(git_dir
));
334 set_git_dir(real_path(real_git_dir
));
337 set_git_dir(real_path(git_dir
));
343 static void separate_git_dir(const char *git_dir
)
348 if (!stat(git_link
, &st
)) {
351 if (S_ISREG(st
.st_mode
))
352 src
= read_gitfile(git_link
);
353 else if (S_ISDIR(st
.st_mode
))
356 die(_("unable to handle file type %d"), (int)st
.st_mode
);
358 if (rename(src
, git_dir
))
359 die_errno(_("unable to move %s to %s"), src
, git_dir
);
362 fp
= fopen(git_link
, "w");
364 die(_("Could not create git link %s"), git_link
);
365 fprintf(fp
, "gitdir: %s\n", git_dir
);
369 int init_db(const char *template_dir
, unsigned int flags
)
372 const char *git_dir
= get_git_dir();
375 separate_git_dir(git_dir
);
377 safe_create_dir(git_dir
, 0);
379 init_is_bare_repository
= is_bare_repository();
381 /* Check to see if the repository version is right.
382 * Note that a newly created repository does not have
383 * config file, so this will not fail. What we are catching
384 * is an attempt to reinitialize new repository with an old tool.
386 check_repository_format();
388 reinit
= create_default_files(template_dir
);
390 create_object_directory();
392 if (shared_repository
) {
394 /* We do not spell "group" and such, so that
395 * the configuration can be read by older version
396 * of git. Note, we use octal numbers for new share modes,
397 * and compatibility values for PERM_GROUP and
400 if (shared_repository
< 0)
401 /* force to the mode value */
402 sprintf(buf
, "0%o", -shared_repository
);
403 else if (shared_repository
== PERM_GROUP
)
404 sprintf(buf
, "%d", OLD_PERM_GROUP
);
405 else if (shared_repository
== PERM_EVERYBODY
)
406 sprintf(buf
, "%d", OLD_PERM_EVERYBODY
);
409 git_config_set("core.sharedrepository", buf
);
410 git_config_set("receive.denyNonFastforwards", "true");
413 if (!(flags
& INIT_DB_QUIET
)) {
414 int len
= strlen(git_dir
);
416 /* TRANSLATORS: The first '%s' is either "Reinitialized
417 existing" or "Initialized empty", the second " shared" or
418 "", and the last '%s%s' is the verbatim directory name. */
419 printf(_("%s%s Git repository in %s%s\n"),
420 reinit
? _("Reinitialized existing") : _("Initialized empty"),
421 shared_repository
? _(" shared") : "",
422 git_dir
, len
&& git_dir
[len
-1] != '/' ? "/" : "");
428 static int guess_repository_type(const char *git_dir
)
435 * "GIT_DIR=. git init" is always bare.
436 * "GIT_DIR=`pwd` git init" too.
438 if (!strcmp(".", git_dir
))
441 cwd_is_git_dir
= !strcmp(git_dir
, cwd
);
446 * "GIT_DIR=.git or GIT_DIR=something/.git is usually not.
448 if (!strcmp(git_dir
, ".git"))
450 slash
= strrchr(git_dir
, '/');
451 if (slash
&& !strcmp(slash
, "/.git"))
455 * Otherwise it is often bare. At this point
456 * we are just guessing.
461 static int shared_callback(const struct option
*opt
, const char *arg
, int unset
)
463 *((int *) opt
->value
) = (arg
) ? git_config_perm("arg", arg
) : PERM_GROUP
;
467 static const char *const init_db_usage
[] = {
468 N_("git init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]] [directory]"),
473 * If you want to, you can share the DB area with any number of branches.
474 * That has advantages: you can save space by sharing all the SHA1 objects.
475 * On the other hand, it might just make lookup slower and messier. You
476 * be the judge. The default case is to have one DB per managed directory.
478 int cmd_init_db(int argc
, const char **argv
, const char *prefix
)
481 const char *real_git_dir
= NULL
;
482 const char *work_tree
;
483 const char *template_dir
= NULL
;
484 unsigned int flags
= 0;
485 const struct option init_db_options
[] = {
486 OPT_STRING(0, "template", &template_dir
, N_("template-directory"),
487 N_("directory from which templates will be used")),
488 OPT_SET_INT(0, "bare", &is_bare_repository_cfg
,
489 N_("create a bare repository"), 1),
490 { OPTION_CALLBACK
, 0, "shared", &init_shared_repository
,
492 N_("specify that the git repository is to be shared amongst several users"),
493 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
, shared_callback
, 0},
494 OPT_BIT('q', "quiet", &flags
, N_("be quiet"), INIT_DB_QUIET
),
495 OPT_STRING(0, "separate-git-dir", &real_git_dir
, N_("gitdir"),
496 N_("separate git dir from working tree")),
500 argc
= parse_options(argc
, argv
, prefix
, init_db_options
, init_db_usage
, 0);
502 if (real_git_dir
&& !is_absolute_path(real_git_dir
))
503 real_git_dir
= xstrdup(real_path(real_git_dir
));
508 if (chdir(argv
[0]) < 0) {
512 * At this point we haven't read any configuration,
513 * and we know shared_repository should always be 0;
514 * but just in case we play safe.
516 saved
= shared_repository
;
517 shared_repository
= 0;
518 switch (safe_create_leading_directories_const(argv
[0])) {
526 die_errno(_("cannot mkdir %s"), argv
[0]);
529 shared_repository
= saved
;
530 if (mkdir(argv
[0], 0777) < 0)
531 die_errno(_("cannot mkdir %s"), argv
[0]);
535 die_errno(_("cannot chdir to %s"), argv
[0]);
537 } else if (0 < argc
) {
538 usage(init_db_usage
[0]);
540 if (is_bare_repository_cfg
== 1) {
541 char *cwd
= xgetcwd();
542 setenv(GIT_DIR_ENVIRONMENT
, cwd
, argc
> 0);
546 if (init_shared_repository
!= -1)
547 shared_repository
= init_shared_repository
;
550 * GIT_WORK_TREE makes sense only in conjunction with GIT_DIR
551 * without --bare. Catch the error early.
553 git_dir
= getenv(GIT_DIR_ENVIRONMENT
);
554 work_tree
= getenv(GIT_WORK_TREE_ENVIRONMENT
);
555 if ((!git_dir
|| is_bare_repository_cfg
== 1) && work_tree
)
556 die(_("%s (or --work-tree=<directory>) not allowed without "
557 "specifying %s (or --git-dir=<directory>)"),
558 GIT_WORK_TREE_ENVIRONMENT
,
559 GIT_DIR_ENVIRONMENT
);
562 * Set up the default .git directory contents
565 git_dir
= DEFAULT_GIT_DIR_ENVIRONMENT
;
567 if (is_bare_repository_cfg
< 0)
568 is_bare_repository_cfg
= guess_repository_type(git_dir
);
570 if (!is_bare_repository_cfg
) {
571 const char *git_dir_parent
= strrchr(git_dir
, '/');
572 if (git_dir_parent
) {
573 char *rel
= xstrndup(git_dir
, git_dir_parent
- git_dir
);
574 git_work_tree_cfg
= xstrdup(real_path(rel
));
577 if (!git_work_tree_cfg
)
578 git_work_tree_cfg
= xgetcwd();
580 set_git_work_tree(work_tree
);
582 set_git_work_tree(git_work_tree_cfg
);
583 if (access(get_git_work_tree(), X_OK
))
584 die_errno (_("Cannot access work tree '%s'"),
585 get_git_work_tree());
589 set_git_work_tree(work_tree
);
592 set_git_dir_init(git_dir
, real_git_dir
, 1);
594 return init_db(template_dir
, flags
);