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 static void safe_create_dir(const char *dir
, int share
)
15 if (mkdir(dir
, 0777) < 0) {
16 if (errno
!= EEXIST
) {
21 else if (share
&& adjust_shared_perm(dir
))
22 die("Could not make %s writable by group\n", dir
);
25 static int copy_file(const char *dst
, const char *src
, int mode
)
29 mode
= (mode
& 0111) ? 0777 : 0666;
30 if ((fdi
= open(src
, O_RDONLY
)) < 0)
32 if ((fdo
= open(dst
, O_WRONLY
| O_CREAT
| O_EXCL
, mode
)) < 0) {
36 status
= copy_fd(fdi
, fdo
);
39 if (!status
&& adjust_shared_perm(dst
))
45 static void copy_templates_1(char *path
, int baselen
,
46 char *template, int template_baselen
,
51 /* Note: if ".git/hooks" file exists in the repository being
52 * re-initialized, /etc/core-git/templates/hooks/update would
53 * cause git-init-db to fail here. I think this is sane but
54 * it means that the set of templates we ship by default, along
55 * with the way the namespace under .git/ is organized, should
56 * be really carefully chosen.
58 safe_create_dir(path
, 1);
59 while ((de
= readdir(dir
)) != NULL
) {
60 struct stat st_git
, st_template
;
64 if (de
->d_name
[0] == '.')
66 namelen
= strlen(de
->d_name
);
67 if ((PATH_MAX
<= baselen
+ namelen
) ||
68 (PATH_MAX
<= template_baselen
+ namelen
))
69 die("insanely long template name %s", de
->d_name
);
70 memcpy(path
+ baselen
, de
->d_name
, namelen
+1);
71 memcpy(template + template_baselen
, de
->d_name
, namelen
+1);
72 if (lstat(path
, &st_git
)) {
74 die("cannot stat %s", path
);
79 if (lstat(template, &st_template
))
80 die("cannot stat template %s", template);
82 if (S_ISDIR(st_template
.st_mode
)) {
83 DIR *subdir
= opendir(template);
84 int baselen_sub
= baselen
+ namelen
;
85 int template_baselen_sub
= template_baselen
+ namelen
;
87 die("cannot opendir %s", template);
89 template[template_baselen_sub
++] = '/';
91 template[template_baselen_sub
] = 0;
92 copy_templates_1(path
, baselen_sub
,
93 template, template_baselen_sub
,
99 else if (S_ISLNK(st_template
.st_mode
)) {
102 len
= readlink(template, lnk
, sizeof(lnk
));
104 die("cannot readlink %s", template);
105 if (sizeof(lnk
) <= len
)
106 die("insanely long symlink %s", template);
108 if (symlink(lnk
, path
))
109 die("cannot symlink %s %s", lnk
, path
);
111 else if (S_ISREG(st_template
.st_mode
)) {
112 if (copy_file(path
, template, st_template
.st_mode
))
113 die("cannot copy %s to %s", template, path
);
116 error("ignoring template %s", template);
120 static void copy_templates(const char *git_dir
, int len
, const char *template_dir
)
123 char template_path
[PATH_MAX
];
128 template_dir
= DEFAULT_GIT_TEMPLATE_DIR
;
129 strcpy(template_path
, template_dir
);
130 template_len
= strlen(template_path
);
131 if (template_path
[template_len
-1] != '/') {
132 template_path
[template_len
++] = '/';
133 template_path
[template_len
] = 0;
135 dir
= opendir(template_path
);
137 fprintf(stderr
, "warning: templates not found %s\n",
142 /* Make sure that template is from the correct vintage */
143 strcpy(template_path
+ template_len
, "config");
144 repository_format_version
= 0;
145 git_config_from_file(check_repository_format_version
,
147 template_path
[template_len
] = 0;
149 if (repository_format_version
&&
150 repository_format_version
!= GIT_REPO_VERSION
) {
151 fprintf(stderr
, "warning: not copying templates of "
152 "a wrong format version %d from '%s'\n",
153 repository_format_version
,
159 memcpy(path
, git_dir
, len
);
161 copy_templates_1(path
, len
,
162 template_path
, template_len
,
167 static int create_default_files(const char *git_dir
, const char *template_path
)
169 unsigned len
= strlen(git_dir
);
170 static char path
[PATH_MAX
];
171 unsigned char sha1
[20];
173 char repo_version_string
[10];
176 if (len
> sizeof(path
)-50)
177 die("insane git directory %s", git_dir
);
178 memcpy(path
, git_dir
, len
);
180 if (len
&& path
[len
-1] != '/')
184 * Create .git/refs/{heads,tags}
186 strcpy(path
+ len
, "refs");
187 safe_create_dir(path
, 1);
188 strcpy(path
+ len
, "refs/heads");
189 safe_create_dir(path
, 1);
190 strcpy(path
+ len
, "refs/tags");
191 safe_create_dir(path
, 1);
193 /* First copy the templates -- we might have the default
194 * config file there, in which case we would want to read
195 * from it after installing.
198 copy_templates(path
, len
, template_path
);
200 git_config(git_default_config
);
203 * We would have created the above under user's umask -- under
204 * shared-repository settings, we would need to fix them up.
206 if (shared_repository
) {
208 adjust_shared_perm(path
);
209 strcpy(path
+ len
, "refs");
210 adjust_shared_perm(path
);
211 strcpy(path
+ len
, "refs/heads");
212 adjust_shared_perm(path
);
213 strcpy(path
+ len
, "refs/tags");
214 adjust_shared_perm(path
);
218 * Create the default symlink from ".git/HEAD" to the "master"
219 * branch, if it does not exist yet.
221 strcpy(path
+ len
, "HEAD");
222 reinit
= !read_ref("HEAD", sha1
);
224 if (create_symref("HEAD", "refs/heads/master") < 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 if (!lstat(path
, &st1
)) {
238 int filemode
= (!chmod(path
, st1
.st_mode
^ S_IXUSR
) &&
239 !lstat(path
, &st2
) &&
240 st1
.st_mode
!= st2
.st_mode
);
241 git_config_set("core.filemode",
242 filemode
? "true" : "false");
245 /* Enable logAllRefUpdates if a working tree is attached */
246 if (!is_bare_git_dir(git_dir
))
247 git_config_set("core.logallrefupdates", "true");
251 static const char init_db_usage
[] =
252 "git-init-db [--template=<template-directory>] [--shared]";
255 * If you want to, you can share the DB area with any number of branches.
256 * That has advantages: you can save space by sharing all the SHA1 objects.
257 * On the other hand, it might just make lookup slower and messier. You
258 * be the judge. The default case is to have one DB per managed directory.
260 int cmd_init_db(int argc
, const char **argv
, const char *prefix
)
263 const char *sha1_dir
;
264 const char *template_dir
= NULL
;
268 for (i
= 1; i
< argc
; i
++, argv
++) {
269 const char *arg
= argv
[1];
270 if (!strncmp(arg
, "--template=", 11))
271 template_dir
= arg
+11;
272 else if (!strcmp(arg
, "--shared"))
273 shared_repository
= PERM_GROUP
;
274 else if (!strncmp(arg
, "--shared=", 9))
275 shared_repository
= git_config_perm("arg", arg
+9);
277 usage(init_db_usage
);
281 * Set up the default .git directory contents
283 git_dir
= getenv(GIT_DIR_ENVIRONMENT
);
285 git_dir
= DEFAULT_GIT_DIR_ENVIRONMENT
;
286 safe_create_dir(git_dir
, 0);
288 /* Check to see if the repository version is right.
289 * Note that a newly created repository does not have
290 * config file, so this will not fail. What we are catching
291 * is an attempt to reinitialize new repository with an old tool.
293 check_repository_format();
295 reinit
= create_default_files(git_dir
, template_dir
);
298 * And set up the object store.
300 sha1_dir
= get_object_directory();
301 len
= strlen(sha1_dir
);
302 path
= xmalloc(len
+ 40);
303 memcpy(path
, sha1_dir
, len
);
305 safe_create_dir(sha1_dir
, 1);
306 strcpy(path
+len
, "/pack");
307 safe_create_dir(path
, 1);
308 strcpy(path
+len
, "/info");
309 safe_create_dir(path
, 1);
311 if (shared_repository
) {
313 /* We do not spell "group" and such, so that
314 * the configuration can be read by older version
317 sprintf(buf
, "%d", shared_repository
);
318 git_config_set("core.sharedrepository", buf
);
319 git_config_set("receive.denyNonFastforwards", "true");
322 printf("%s%s Git repository in %s/\n",
323 reinit
? "Reinitialized existing" : "Initialized empty",
324 shared_repository
? " shared" : "",