2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
10 #ifndef DEFAULT_GIT_TEMPLATE_DIR
11 #define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates"
14 #ifdef NO_TRUSTABLE_FILEMODE
15 #define TEST_FILEMODE 0
17 #define TEST_FILEMODE 1
20 static int init_is_bare_repository
= 0;
21 static int init_shared_repository
= -1;
23 static void safe_create_dir(const char *dir
, int share
)
25 if (mkdir(dir
, 0777) < 0) {
26 if (errno
!= EEXIST
) {
31 else if (share
&& adjust_shared_perm(dir
))
32 die("Could not make %s writable by group", dir
);
35 static void copy_templates_1(char *path
, int baselen
,
36 char *template, int template_baselen
,
41 /* Note: if ".git/hooks" file exists in the repository being
42 * re-initialized, /etc/core-git/templates/hooks/update would
43 * cause "git init" to fail here. I think this is sane but
44 * it means that the set of templates we ship by default, along
45 * with the way the namespace under .git/ is organized, should
46 * be really carefully chosen.
48 safe_create_dir(path
, 1);
49 while ((de
= readdir(dir
)) != NULL
) {
50 struct stat st_git
, st_template
;
54 if (de
->d_name
[0] == '.')
56 namelen
= strlen(de
->d_name
);
57 if ((PATH_MAX
<= baselen
+ namelen
) ||
58 (PATH_MAX
<= template_baselen
+ namelen
))
59 die("insanely long template name %s", de
->d_name
);
60 memcpy(path
+ baselen
, de
->d_name
, namelen
+1);
61 memcpy(template + template_baselen
, de
->d_name
, namelen
+1);
62 if (lstat(path
, &st_git
)) {
64 die_errno("cannot stat '%s'", path
);
69 if (lstat(template, &st_template
))
70 die_errno("cannot stat template '%s'", template);
72 if (S_ISDIR(st_template
.st_mode
)) {
73 DIR *subdir
= opendir(template);
74 int baselen_sub
= baselen
+ namelen
;
75 int template_baselen_sub
= template_baselen
+ namelen
;
77 die_errno("cannot opendir '%s'", template);
79 template[template_baselen_sub
++] = '/';
81 template[template_baselen_sub
] = 0;
82 copy_templates_1(path
, baselen_sub
,
83 template, template_baselen_sub
,
89 else if (S_ISLNK(st_template
.st_mode
)) {
92 len
= readlink(template, lnk
, sizeof(lnk
));
94 die_errno("cannot readlink '%s'", template);
95 if (sizeof(lnk
) <= len
)
96 die("insanely long symlink %s", template);
98 if (symlink(lnk
, path
))
99 die_errno("cannot symlink '%s' '%s'", lnk
, path
);
101 else if (S_ISREG(st_template
.st_mode
)) {
102 if (copy_file(path
, template, st_template
.st_mode
))
103 die_errno("cannot copy '%s' to '%s'", template,
107 error("ignoring template %s", template);
111 static void copy_templates(const char *template_dir
)
114 char template_path
[PATH_MAX
];
117 const char *git_dir
= get_git_dir();
118 int len
= strlen(git_dir
);
121 template_dir
= getenv(TEMPLATE_DIR_ENVIRONMENT
);
123 template_dir
= system_path(DEFAULT_GIT_TEMPLATE_DIR
);
124 if (!template_dir
[0])
126 template_len
= strlen(template_dir
);
127 if (PATH_MAX
<= (template_len
+strlen("/config")))
128 die("insanely long template path %s", template_dir
);
129 strcpy(template_path
, template_dir
);
130 if (template_path
[template_len
-1] != '/') {
131 template_path
[template_len
++] = '/';
132 template_path
[template_len
] = 0;
134 dir
= opendir(template_path
);
136 warning("templates not found %s", template_dir
);
140 /* Make sure that template is from the correct vintage */
141 strcpy(template_path
+ template_len
, "config");
142 repository_format_version
= 0;
143 git_config_from_file(check_repository_format_version
,
144 template_path
, NULL
);
145 template_path
[template_len
] = 0;
147 if (repository_format_version
&&
148 repository_format_version
!= GIT_REPO_VERSION
) {
149 warning("not copying templates of "
150 "a wrong format version %d from '%s'",
151 repository_format_version
,
157 memcpy(path
, git_dir
, len
);
158 if (len
&& path
[len
- 1] != '/')
161 copy_templates_1(path
, len
,
162 template_path
, template_len
,
167 static int create_default_files(const char *template_path
)
169 const char *git_dir
= get_git_dir();
170 unsigned len
= strlen(git_dir
);
171 static char path
[PATH_MAX
];
173 char repo_version_string
[10];
178 if (len
> sizeof(path
)-50)
179 die("insane git directory %s", git_dir
);
180 memcpy(path
, git_dir
, len
);
182 if (len
&& path
[len
-1] != '/')
186 * Create .git/refs/{heads,tags}
188 safe_create_dir(git_path("refs"), 1);
189 safe_create_dir(git_path("refs/heads"), 1);
190 safe_create_dir(git_path("refs/tags"), 1);
192 /* First copy the templates -- we might have the default
193 * config file there, in which case we would want to read
194 * from it after installing.
196 copy_templates(template_path
);
198 git_config(git_default_config
, NULL
);
199 is_bare_repository_cfg
= init_is_bare_repository
;
201 /* reading existing config may have overwrote it */
202 if (init_shared_repository
!= -1)
203 shared_repository
= init_shared_repository
;
206 * We would have created the above under user's umask -- under
207 * shared-repository settings, we would need to fix them up.
209 if (shared_repository
) {
210 adjust_shared_perm(get_git_dir());
211 adjust_shared_perm(git_path("refs"));
212 adjust_shared_perm(git_path("refs/heads"));
213 adjust_shared_perm(git_path("refs/tags"));
217 * Create the default symlink from ".git/HEAD" to the "master"
218 * branch, if it does not exist yet.
220 strcpy(path
+ len
, "HEAD");
221 reinit
= (!access(path
, R_OK
)
222 || readlink(path
, junk
, sizeof(junk
)-1) != -1);
224 if (create_symref("HEAD", "refs/heads/master", NULL
) < 0)
228 /* This forces creation of new config file */
229 sprintf(repo_version_string
, "%d", GIT_REPO_VERSION
);
230 git_config_set("core.repositoryformatversion", repo_version_string
);
233 strcpy(path
+ len
, "config");
235 /* Check filemode trustability */
236 filemode
= TEST_FILEMODE
;
237 if (TEST_FILEMODE
&& !lstat(path
, &st1
)) {
239 filemode
= (!chmod(path
, st1
.st_mode
^ S_IXUSR
) &&
240 !lstat(path
, &st2
) &&
241 st1
.st_mode
!= st2
.st_mode
);
243 git_config_set("core.filemode", filemode
? "true" : "false");
245 if (is_bare_repository())
246 git_config_set("core.bare", "true");
248 const char *work_tree
= get_git_work_tree();
249 git_config_set("core.bare", "false");
250 /* allow template config file to override the default */
251 if (log_all_ref_updates
== -1)
252 git_config_set("core.logallrefupdates", "true");
253 if (prefixcmp(git_dir
, work_tree
) ||
254 strcmp(git_dir
+ strlen(work_tree
), "/.git")) {
255 git_config_set("core.worktree", work_tree
);
260 /* Check if symlink is supported in the work tree */
262 strcpy(path
+ len
, "tXXXXXX");
263 if (!close(xmkstemp(path
)) &&
265 !symlink("testing", path
) &&
266 !lstat(path
, &st1
) &&
267 S_ISLNK(st1
.st_mode
))
268 unlink(path
); /* good */
270 git_config_set("core.symlinks", "false");
272 /* Check if the filesystem is case-insensitive */
274 strcpy(path
+ len
, "CoNfIg");
275 if (!access(path
, F_OK
))
276 git_config_set("core.ignorecase", "true");
282 int init_db(const char *template_dir
, unsigned int flags
)
284 const char *sha1_dir
;
288 safe_create_dir(get_git_dir(), 0);
290 init_is_bare_repository
= is_bare_repository();
292 /* Check to see if the repository version is right.
293 * Note that a newly created repository does not have
294 * config file, so this will not fail. What we are catching
295 * is an attempt to reinitialize new repository with an old tool.
297 check_repository_format();
299 reinit
= create_default_files(template_dir
);
301 sha1_dir
= get_object_directory();
302 len
= strlen(sha1_dir
);
303 path
= xmalloc(len
+ 40);
304 memcpy(path
, sha1_dir
, len
);
306 safe_create_dir(sha1_dir
, 1);
307 strcpy(path
+len
, "/pack");
308 safe_create_dir(path
, 1);
309 strcpy(path
+len
, "/info");
310 safe_create_dir(path
, 1);
312 if (shared_repository
) {
314 /* We do not spell "group" and such, so that
315 * the configuration can be read by older version
316 * of git. Note, we use octal numbers for new share modes,
317 * and compatibility values for PERM_GROUP and
320 if (shared_repository
< 0)
321 /* force to the mode value */
322 sprintf(buf
, "0%o", -shared_repository
);
323 else if (shared_repository
== PERM_GROUP
)
324 sprintf(buf
, "%d", OLD_PERM_GROUP
);
325 else if (shared_repository
== PERM_EVERYBODY
)
326 sprintf(buf
, "%d", OLD_PERM_EVERYBODY
);
329 git_config_set("core.sharedrepository", buf
);
330 git_config_set("receive.denyNonFastforwards", "true");
333 if (!(flags
& INIT_DB_QUIET
))
334 printf("%s%s Git repository in %s/\n",
335 reinit
? "Reinitialized existing" : "Initialized empty",
336 shared_repository
? " shared" : "",
342 static int guess_repository_type(const char *git_dir
)
348 * "GIT_DIR=. git init" is always bare.
349 * "GIT_DIR=`pwd` git init" too.
351 if (!strcmp(".", git_dir
))
353 if (!getcwd(cwd
, sizeof(cwd
)))
354 die_errno("cannot tell cwd");
355 if (!strcmp(git_dir
, cwd
))
358 * "GIT_DIR=.git or GIT_DIR=something/.git is usually not.
360 if (!strcmp(git_dir
, ".git"))
362 slash
= strrchr(git_dir
, '/');
363 if (slash
&& !strcmp(slash
, "/.git"))
367 * Otherwise it is often bare. At this point
368 * we are just guessing.
373 static const char init_db_usage
[] =
374 "git init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]] [directory]";
377 * If you want to, you can share the DB area with any number of branches.
378 * That has advantages: you can save space by sharing all the SHA1 objects.
379 * On the other hand, it might just make lookup slower and messier. You
380 * be the judge. The default case is to have one DB per managed directory.
382 int cmd_init_db(int argc
, const char **argv
, const char *prefix
)
385 const char *template_dir
= NULL
;
386 unsigned int flags
= 0;
390 for (i
= 1; i
< argc
; i
++, argv
++) {
391 const char *arg
= argv
[1];
392 if (!prefixcmp(arg
, "--template="))
393 template_dir
= arg
+11;
394 else if (!strcmp(arg
, "--bare"))
395 bare_given
= is_bare_repository_cfg
= 1;
396 else if (!strcmp(arg
, "--shared"))
397 init_shared_repository
= PERM_GROUP
;
398 else if (!prefixcmp(arg
, "--shared="))
399 init_shared_repository
= git_config_perm("arg", arg
+9);
400 else if (!strcmp(arg
, "-q") || !strcmp(arg
, "--quiet"))
401 flags
|= INIT_DB_QUIET
;
402 else if (arg
[0] == '-')
403 usage(init_db_usage
);
411 if (chdir(argv
[1]) < 0) {
415 * At this point we haven't read any configuration,
416 * and we know shared_repository should always be 0;
417 * but just in case we play safe.
419 saved
= shared_repository
;
420 shared_repository
= 0;
421 switch (safe_create_leading_directories_const(argv
[1])) {
426 die_errno("cannot mkdir %s", argv
[1]);
431 shared_repository
= saved
;
432 if (mkdir(argv
[1], 0777) < 0)
433 die_errno("cannot mkdir %s", argv
[1]);
437 die_errno("cannot chdir to %s", argv
[1]);
439 } else if (i
< argc
- 1) {
440 usage(init_db_usage
);
442 if (bare_given
== 1) {
443 static char git_dir
[PATH_MAX
+1];
445 setenv(GIT_DIR_ENVIRONMENT
,
446 getcwd(git_dir
, sizeof(git_dir
)), 0);
448 if (init_shared_repository
!= -1)
449 shared_repository
= init_shared_repository
;
452 * GIT_WORK_TREE makes sense only in conjunction with GIT_DIR
453 * without --bare. Catch the error early.
455 git_dir
= getenv(GIT_DIR_ENVIRONMENT
);
456 if ((!git_dir
|| is_bare_repository_cfg
== 1)
457 && getenv(GIT_WORK_TREE_ENVIRONMENT
))
458 die("%s (or --work-tree=<directory>) not allowed without "
459 "specifying %s (or --git-dir=<directory>)",
460 GIT_WORK_TREE_ENVIRONMENT
,
461 GIT_DIR_ENVIRONMENT
);
464 * Set up the default .git directory contents
467 git_dir
= DEFAULT_GIT_DIR_ENVIRONMENT
;
469 if (is_bare_repository_cfg
< 0)
470 is_bare_repository_cfg
= guess_repository_type(git_dir
);
472 if (!is_bare_repository_cfg
) {
474 const char *git_dir_parent
= strrchr(git_dir
, '/');
475 if (git_dir_parent
) {
476 char *rel
= xstrndup(git_dir
, git_dir_parent
- git_dir
);
477 git_work_tree_cfg
= xstrdup(make_absolute_path(rel
));
481 if (!git_work_tree_cfg
) {
482 git_work_tree_cfg
= xcalloc(PATH_MAX
, 1);
483 if (!getcwd(git_work_tree_cfg
, PATH_MAX
))
484 die_errno ("Cannot access current working directory");
486 if (access(get_git_work_tree(), X_OK
))
487 die_errno ("Cannot access work tree '%s'",
488 get_git_work_tree());
491 set_git_dir(make_absolute_path(git_dir
));
493 return init_db(template_dir
, flags
);