2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
9 #ifndef DEFAULT_GIT_TEMPLATE_DIR
10 #define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates/"
13 #ifdef NO_TRUSTABLE_FILEMODE
14 #define TEST_FILEMODE 0
16 #define TEST_FILEMODE 1
19 static void safe_create_dir(const char *dir
, int share
)
21 if (mkdir(dir
, 0777) < 0) {
22 if (errno
!= EEXIST
) {
27 else if (share
&& adjust_shared_perm(dir
))
28 die("Could not make %s writable by group\n", dir
);
31 static int copy_file(const char *dst
, const char *src
, int mode
)
35 mode
= (mode
& 0111) ? 0777 : 0666;
36 if ((fdi
= open(src
, O_RDONLY
)) < 0)
38 if ((fdo
= open(dst
, O_WRONLY
| O_CREAT
| O_EXCL
, mode
)) < 0) {
42 status
= copy_fd(fdi
, fdo
);
45 if (!status
&& adjust_shared_perm(dst
))
51 static void copy_templates_1(char *path
, int baselen
,
52 char *template, int template_baselen
,
57 /* Note: if ".git/hooks" file exists in the repository being
58 * re-initialized, /etc/core-git/templates/hooks/update would
59 * cause git-init-db to fail here. I think this is sane but
60 * it means that the set of templates we ship by default, along
61 * with the way the namespace under .git/ is organized, should
62 * be really carefully chosen.
64 safe_create_dir(path
, 1);
65 while ((de
= readdir(dir
)) != NULL
) {
66 struct stat st_git
, st_template
;
70 if (de
->d_name
[0] == '.')
72 namelen
= strlen(de
->d_name
);
73 if ((PATH_MAX
<= baselen
+ namelen
) ||
74 (PATH_MAX
<= template_baselen
+ namelen
))
75 die("insanely long template name %s", de
->d_name
);
76 memcpy(path
+ baselen
, de
->d_name
, namelen
+1);
77 memcpy(template + template_baselen
, de
->d_name
, namelen
+1);
78 if (lstat(path
, &st_git
)) {
80 die("cannot stat %s", path
);
85 if (lstat(template, &st_template
))
86 die("cannot stat template %s", template);
88 if (S_ISDIR(st_template
.st_mode
)) {
89 DIR *subdir
= opendir(template);
90 int baselen_sub
= baselen
+ namelen
;
91 int template_baselen_sub
= template_baselen
+ namelen
;
93 die("cannot opendir %s", template);
95 template[template_baselen_sub
++] = '/';
97 template[template_baselen_sub
] = 0;
98 copy_templates_1(path
, baselen_sub
,
99 template, template_baselen_sub
,
105 else if (S_ISLNK(st_template
.st_mode
)) {
108 len
= readlink(template, lnk
, sizeof(lnk
));
110 die("cannot readlink %s", template);
111 if (sizeof(lnk
) <= len
)
112 die("insanely long symlink %s", template);
114 if (symlink(lnk
, path
))
115 die("cannot symlink %s %s", lnk
, path
);
117 else if (S_ISREG(st_template
.st_mode
)) {
118 if (copy_file(path
, template, st_template
.st_mode
))
119 die("cannot copy %s to %s", template, path
);
122 error("ignoring template %s", template);
126 static void copy_templates(const char *git_dir
, int len
, const char *template_dir
)
129 char template_path
[PATH_MAX
];
134 template_dir
= getenv(TEMPLATE_DIR_ENVIRONMENT
);
136 template_dir
= DEFAULT_GIT_TEMPLATE_DIR
;
138 strcpy(template_path
, template_dir
);
139 template_len
= strlen(template_path
);
140 if (template_path
[template_len
-1] != '/') {
141 template_path
[template_len
++] = '/';
142 template_path
[template_len
] = 0;
144 dir
= opendir(template_path
);
146 fprintf(stderr
, "warning: templates not found %s\n",
151 /* Make sure that template is from the correct vintage */
152 strcpy(template_path
+ template_len
, "config");
153 repository_format_version
= 0;
154 git_config_from_file(check_repository_format_version
,
156 template_path
[template_len
] = 0;
158 if (repository_format_version
&&
159 repository_format_version
!= GIT_REPO_VERSION
) {
160 fprintf(stderr
, "warning: not copying templates of "
161 "a wrong format version %d from '%s'\n",
162 repository_format_version
,
168 memcpy(path
, git_dir
, len
);
170 copy_templates_1(path
, len
,
171 template_path
, template_len
,
176 static int create_default_files(const char *git_dir
, const char *template_path
)
178 unsigned len
= strlen(git_dir
);
179 static char path
[PATH_MAX
];
180 unsigned char sha1
[20];
182 char repo_version_string
[10];
186 if (len
> sizeof(path
)-50)
187 die("insane git directory %s", git_dir
);
188 memcpy(path
, git_dir
, len
);
190 if (len
&& path
[len
-1] != '/')
194 * Create .git/refs/{heads,tags}
196 strcpy(path
+ len
, "refs");
197 safe_create_dir(path
, 1);
198 strcpy(path
+ len
, "refs/heads");
199 safe_create_dir(path
, 1);
200 strcpy(path
+ len
, "refs/tags");
201 safe_create_dir(path
, 1);
203 /* First copy the templates -- we might have the default
204 * config file there, in which case we would want to read
205 * from it after installing.
208 copy_templates(path
, len
, template_path
);
210 git_config(git_default_config
);
213 * We would have created the above under user's umask -- under
214 * shared-repository settings, we would need to fix them up.
216 if (shared_repository
) {
218 adjust_shared_perm(path
);
219 strcpy(path
+ len
, "refs");
220 adjust_shared_perm(path
);
221 strcpy(path
+ len
, "refs/heads");
222 adjust_shared_perm(path
);
223 strcpy(path
+ len
, "refs/tags");
224 adjust_shared_perm(path
);
228 * Create the default symlink from ".git/HEAD" to the "master"
229 * branch, if it does not exist yet.
231 strcpy(path
+ len
, "HEAD");
232 reinit
= !read_ref("HEAD", sha1
);
234 if (create_symref("HEAD", "refs/heads/master") < 0)
238 /* This forces creation of new config file */
239 sprintf(repo_version_string
, "%d", GIT_REPO_VERSION
);
240 git_config_set("core.repositoryformatversion", repo_version_string
);
243 strcpy(path
+ len
, "config");
245 /* Check filemode trustability */
246 filemode
= TEST_FILEMODE
;
247 if (TEST_FILEMODE
&& !lstat(path
, &st1
)) {
249 filemode
= (!chmod(path
, st1
.st_mode
^ S_IXUSR
) &&
250 !lstat(path
, &st2
) &&
251 st1
.st_mode
!= st2
.st_mode
);
253 git_config_set("core.filemode", filemode
? "true" : "false");
255 if (is_bare_repository()) {
256 git_config_set("core.bare", "true");
259 git_config_set("core.bare", "false");
260 git_config_set("core.logallrefupdates", "true");
265 static const char init_db_usage
[] =
266 "git-init [--template=<template-directory>] [--shared]";
269 * If you want to, you can share the DB area with any number of branches.
270 * That has advantages: you can save space by sharing all the SHA1 objects.
271 * On the other hand, it might just make lookup slower and messier. You
272 * be the judge. The default case is to have one DB per managed directory.
274 int cmd_init_db(int argc
, const char **argv
, const char *prefix
)
277 const char *sha1_dir
;
278 const char *template_dir
= NULL
;
282 for (i
= 1; i
< argc
; i
++, argv
++) {
283 const char *arg
= argv
[1];
284 if (!strncmp(arg
, "--template=", 11))
285 template_dir
= arg
+11;
286 else if (!strcmp(arg
, "--shared"))
287 shared_repository
= PERM_GROUP
;
288 else if (!strncmp(arg
, "--shared=", 9))
289 shared_repository
= git_config_perm("arg", arg
+9);
291 usage(init_db_usage
);
295 * Set up the default .git directory contents
297 git_dir
= getenv(GIT_DIR_ENVIRONMENT
);
299 git_dir
= DEFAULT_GIT_DIR_ENVIRONMENT
;
300 safe_create_dir(git_dir
, 0);
302 /* Check to see if the repository version is right.
303 * Note that a newly created repository does not have
304 * config file, so this will not fail. What we are catching
305 * is an attempt to reinitialize new repository with an old tool.
307 check_repository_format();
309 reinit
= create_default_files(git_dir
, template_dir
);
312 * And set up the object store.
314 sha1_dir
= get_object_directory();
315 len
= strlen(sha1_dir
);
316 path
= xmalloc(len
+ 40);
317 memcpy(path
, sha1_dir
, len
);
319 safe_create_dir(sha1_dir
, 1);
320 strcpy(path
+len
, "/pack");
321 safe_create_dir(path
, 1);
322 strcpy(path
+len
, "/info");
323 safe_create_dir(path
, 1);
325 if (shared_repository
) {
327 /* We do not spell "group" and such, so that
328 * the configuration can be read by older version
331 sprintf(buf
, "%d", shared_repository
);
332 git_config_set("core.sharedrepository", buf
);
333 git_config_set("receive.denyNonFastforwards", "true");
336 printf("%s%s Git repository in %s/\n",
337 reinit
? "Reinitialized existing" : "Initialized empty",
338 shared_repository
? " shared" : "",