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 void safe_create_dir(const char *dir
, int share
)
22 if (mkdir(dir
, 0777) < 0) {
23 if (errno
!= EEXIST
) {
28 else if (share
&& adjust_shared_perm(dir
))
29 die("Could not make %s writable by group\n", dir
);
32 static int copy_file(const char *dst
, const char *src
, int mode
)
36 mode
= (mode
& 0111) ? 0777 : 0666;
37 if ((fdi
= open(src
, O_RDONLY
)) < 0)
39 if ((fdo
= open(dst
, O_WRONLY
| O_CREAT
| O_EXCL
, mode
)) < 0) {
43 status
= copy_fd(fdi
, fdo
);
45 return error("%s: write error: %s", dst
, strerror(errno
));
47 if (!status
&& adjust_shared_perm(dst
))
53 static void copy_templates_1(char *path
, int baselen
,
54 char *template, int template_baselen
,
59 /* Note: if ".git/hooks" file exists in the repository being
60 * re-initialized, /etc/core-git/templates/hooks/update would
61 * cause git-init to fail here. I think this is sane but
62 * it means that the set of templates we ship by default, along
63 * with the way the namespace under .git/ is organized, should
64 * be really carefully chosen.
66 safe_create_dir(path
, 1);
67 while ((de
= readdir(dir
)) != NULL
) {
68 struct stat st_git
, st_template
;
72 if (de
->d_name
[0] == '.')
74 namelen
= strlen(de
->d_name
);
75 if ((PATH_MAX
<= baselen
+ namelen
) ||
76 (PATH_MAX
<= template_baselen
+ namelen
))
77 die("insanely long template name %s", de
->d_name
);
78 memcpy(path
+ baselen
, de
->d_name
, namelen
+1);
79 memcpy(template + template_baselen
, de
->d_name
, namelen
+1);
80 if (lstat(path
, &st_git
)) {
82 die("cannot stat %s", path
);
87 if (lstat(template, &st_template
))
88 die("cannot stat template %s", template);
90 if (S_ISDIR(st_template
.st_mode
)) {
91 DIR *subdir
= opendir(template);
92 int baselen_sub
= baselen
+ namelen
;
93 int template_baselen_sub
= template_baselen
+ namelen
;
95 die("cannot opendir %s", template);
97 template[template_baselen_sub
++] = '/';
99 template[template_baselen_sub
] = 0;
100 copy_templates_1(path
, baselen_sub
,
101 template, template_baselen_sub
,
107 else if (S_ISLNK(st_template
.st_mode
)) {
110 len
= readlink(template, lnk
, sizeof(lnk
));
112 die("cannot readlink %s", template);
113 if (sizeof(lnk
) <= len
)
114 die("insanely long symlink %s", template);
116 if (symlink(lnk
, path
))
117 die("cannot symlink %s %s", lnk
, path
);
119 else if (S_ISREG(st_template
.st_mode
)) {
120 if (copy_file(path
, template, st_template
.st_mode
))
121 die("cannot copy %s to %s", template, path
);
124 error("ignoring template %s", template);
128 static void copy_templates(const char *git_dir
, int len
, const char *template_dir
)
131 char template_path
[PATH_MAX
];
136 template_dir
= getenv(TEMPLATE_DIR_ENVIRONMENT
);
139 * if the hard-coded template is relative, it is
140 * interpreted relative to the exec_dir
142 template_dir
= DEFAULT_GIT_TEMPLATE_DIR
;
143 if (!is_absolute_path(template_dir
)) {
144 struct strbuf d
= STRBUF_INIT
;
145 strbuf_addf(&d
, "%s/%s", git_exec_path(), template_dir
);
146 template_dir
= strbuf_detach(&d
, NULL
);
149 strcpy(template_path
, template_dir
);
150 template_len
= strlen(template_path
);
151 if (template_path
[template_len
-1] != '/') {
152 template_path
[template_len
++] = '/';
153 template_path
[template_len
] = 0;
155 dir
= opendir(template_path
);
157 fprintf(stderr
, "warning: templates not found %s\n",
162 /* Make sure that template is from the correct vintage */
163 strcpy(template_path
+ template_len
, "config");
164 repository_format_version
= 0;
165 git_config_from_file(check_repository_format_version
,
167 template_path
[template_len
] = 0;
169 if (repository_format_version
&&
170 repository_format_version
!= GIT_REPO_VERSION
) {
171 fprintf(stderr
, "warning: not copying templates of "
172 "a wrong format version %d from '%s'\n",
173 repository_format_version
,
179 memcpy(path
, git_dir
, len
);
181 copy_templates_1(path
, len
,
182 template_path
, template_len
,
187 static int create_default_files(const char *git_dir
, const char *template_path
)
189 unsigned len
= strlen(git_dir
);
190 static char path
[PATH_MAX
];
191 unsigned char sha1
[20];
193 char repo_version_string
[10];
197 if (len
> sizeof(path
)-50)
198 die("insane git directory %s", git_dir
);
199 memcpy(path
, git_dir
, len
);
201 if (len
&& path
[len
-1] != '/')
205 * Create .git/refs/{heads,tags}
207 strcpy(path
+ len
, "refs");
208 safe_create_dir(path
, 1);
209 strcpy(path
+ len
, "refs/heads");
210 safe_create_dir(path
, 1);
211 strcpy(path
+ len
, "refs/tags");
212 safe_create_dir(path
, 1);
214 /* First copy the templates -- we might have the default
215 * config file there, in which case we would want to read
216 * from it after installing.
219 copy_templates(path
, len
, template_path
);
221 git_config(git_default_config
);
224 * We would have created the above under user's umask -- under
225 * shared-repository settings, we would need to fix them up.
227 if (shared_repository
) {
229 adjust_shared_perm(path
);
230 strcpy(path
+ len
, "refs");
231 adjust_shared_perm(path
);
232 strcpy(path
+ len
, "refs/heads");
233 adjust_shared_perm(path
);
234 strcpy(path
+ len
, "refs/tags");
235 adjust_shared_perm(path
);
239 * Create the default symlink from ".git/HEAD" to the "master"
240 * branch, if it does not exist yet.
242 strcpy(path
+ len
, "HEAD");
243 reinit
= !read_ref("HEAD", sha1
);
245 if (create_symref("HEAD", "refs/heads/master", NULL
) < 0)
249 /* This forces creation of new config file */
250 sprintf(repo_version_string
, "%d", GIT_REPO_VERSION
);
251 git_config_set("core.repositoryformatversion", repo_version_string
);
254 strcpy(path
+ len
, "config");
256 /* Check filemode trustability */
257 filemode
= TEST_FILEMODE
;
258 if (TEST_FILEMODE
&& !lstat(path
, &st1
)) {
260 filemode
= (!chmod(path
, st1
.st_mode
^ S_IXUSR
) &&
261 !lstat(path
, &st2
) &&
262 st1
.st_mode
!= st2
.st_mode
);
264 git_config_set("core.filemode", filemode
? "true" : "false");
266 if (is_bare_repository())
267 git_config_set("core.bare", "true");
269 const char *work_tree
= get_git_work_tree();
270 git_config_set("core.bare", "false");
271 /* allow template config file to override the default */
272 if (log_all_ref_updates
== -1)
273 git_config_set("core.logallrefupdates", "true");
274 if (work_tree
!= git_work_tree_cfg
)
275 git_config_set("core.worktree", work_tree
);
278 /* Check if symlink is supported in the work tree */
281 strcpy(path
+ len
, "tXXXXXX");
282 if (!close(xmkstemp(path
)) &&
284 !symlink("testing", path
) &&
285 !lstat(path
, &st1
) &&
286 S_ISLNK(st1
.st_mode
))
287 unlink(path
); /* good */
289 git_config_set("core.symlinks", "false");
295 static void guess_repository_type(const char *git_dir
)
300 if (0 <= is_bare_repository_cfg
)
306 * "GIT_DIR=. git init" is always bare.
307 * "GIT_DIR=`pwd` git init" too.
309 if (!strcmp(".", git_dir
))
311 if (!getcwd(cwd
, sizeof(cwd
)))
312 die("cannot tell cwd");
313 if (!strcmp(git_dir
, cwd
))
316 * "GIT_DIR=.git or GIT_DIR=something/.git is usually not.
318 if (!strcmp(git_dir
, ".git"))
320 slash
= strrchr(git_dir
, '/');
321 if (slash
&& !strcmp(slash
, "/.git"))
325 * Otherwise it is often bare. At this point
326 * we are just guessing.
329 is_bare_repository_cfg
= 1;
333 static const char init_db_usage
[] =
334 "git-init [-q | --quiet] [--template=<template-directory>] [--shared]";
337 * If you want to, you can share the DB area with any number of branches.
338 * That has advantages: you can save space by sharing all the SHA1 objects.
339 * On the other hand, it might just make lookup slower and messier. You
340 * be the judge. The default case is to have one DB per managed directory.
342 int cmd_init_db(int argc
, const char **argv
, const char *prefix
)
345 const char *sha1_dir
;
346 const char *template_dir
= NULL
;
351 for (i
= 1; i
< argc
; i
++, argv
++) {
352 const char *arg
= argv
[1];
353 if (!prefixcmp(arg
, "--template="))
354 template_dir
= arg
+11;
355 else if (!strcmp(arg
, "--shared"))
356 shared_repository
= PERM_GROUP
;
357 else if (!prefixcmp(arg
, "--shared="))
358 shared_repository
= git_config_perm("arg", arg
+9);
359 else if (!strcmp(arg
, "-q") || !strcmp(arg
, "--quiet"))
362 usage(init_db_usage
);
366 * GIT_WORK_TREE makes sense only in conjunction with GIT_DIR
367 * without --bare. Catch the error early.
369 git_dir
= getenv(GIT_DIR_ENVIRONMENT
);
370 if ((!git_dir
|| is_bare_repository_cfg
== 1)
371 && getenv(GIT_WORK_TREE_ENVIRONMENT
))
372 die("%s (or --work-tree=<directory>) not allowed without "
373 "specifying %s (or --git-dir=<directory>)",
374 GIT_WORK_TREE_ENVIRONMENT
,
375 GIT_DIR_ENVIRONMENT
);
377 guess_repository_type(git_dir
);
379 if (is_bare_repository_cfg
<= 0) {
380 git_work_tree_cfg
= xcalloc(PATH_MAX
, 1);
381 if (!getcwd(git_work_tree_cfg
, PATH_MAX
))
382 die ("Cannot access current working directory.");
383 if (access(get_git_work_tree(), X_OK
))
384 die ("Cannot access work tree '%s'",
385 get_git_work_tree());
389 * Set up the default .git directory contents
391 git_dir
= getenv(GIT_DIR_ENVIRONMENT
);
393 git_dir
= DEFAULT_GIT_DIR_ENVIRONMENT
;
394 safe_create_dir(git_dir
, 0);
396 /* Check to see if the repository version is right.
397 * Note that a newly created repository does not have
398 * config file, so this will not fail. What we are catching
399 * is an attempt to reinitialize new repository with an old tool.
401 check_repository_format();
403 reinit
= create_default_files(git_dir
, template_dir
);
406 * And set up the object store.
408 sha1_dir
= get_object_directory();
409 len
= strlen(sha1_dir
);
410 path
= xmalloc(len
+ 40);
411 memcpy(path
, sha1_dir
, len
);
413 safe_create_dir(sha1_dir
, 1);
414 strcpy(path
+len
, "/pack");
415 safe_create_dir(path
, 1);
416 strcpy(path
+len
, "/info");
417 safe_create_dir(path
, 1);
419 if (shared_repository
) {
421 /* We do not spell "group" and such, so that
422 * the configuration can be read by older version
425 sprintf(buf
, "%d", shared_repository
);
426 git_config_set("core.sharedrepository", buf
);
427 git_config_set("receive.denyNonFastforwards", "true");
431 printf("%s%s Git repository in %s/\n",
432 reinit
? "Reinitialized existing" : "Initialized empty",
433 shared_repository
? " shared" : "",