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 void copy_templates_1(char *path
, int baselen
,
33 char *template, int template_baselen
,
38 /* Note: if ".git/hooks" file exists in the repository being
39 * re-initialized, /etc/core-git/templates/hooks/update would
40 * cause git-init to fail here. I think this is sane but
41 * it means that the set of templates we ship by default, along
42 * with the way the namespace under .git/ is organized, should
43 * be really carefully chosen.
45 safe_create_dir(path
, 1);
46 while ((de
= readdir(dir
)) != NULL
) {
47 struct stat st_git
, st_template
;
51 if (de
->d_name
[0] == '.')
53 namelen
= strlen(de
->d_name
);
54 if ((PATH_MAX
<= baselen
+ namelen
) ||
55 (PATH_MAX
<= template_baselen
+ namelen
))
56 die("insanely long template name %s", de
->d_name
);
57 memcpy(path
+ baselen
, de
->d_name
, namelen
+1);
58 memcpy(template + template_baselen
, de
->d_name
, namelen
+1);
59 if (lstat(path
, &st_git
)) {
61 die("cannot stat %s", path
);
66 if (lstat(template, &st_template
))
67 die("cannot stat template %s", template);
69 if (S_ISDIR(st_template
.st_mode
)) {
70 DIR *subdir
= opendir(template);
71 int baselen_sub
= baselen
+ namelen
;
72 int template_baselen_sub
= template_baselen
+ namelen
;
74 die("cannot opendir %s", template);
76 template[template_baselen_sub
++] = '/';
78 template[template_baselen_sub
] = 0;
79 copy_templates_1(path
, baselen_sub
,
80 template, template_baselen_sub
,
86 else if (S_ISLNK(st_template
.st_mode
)) {
89 len
= readlink(template, lnk
, sizeof(lnk
));
91 die("cannot readlink %s", template);
92 if (sizeof(lnk
) <= len
)
93 die("insanely long symlink %s", template);
95 if (symlink(lnk
, path
))
96 die("cannot symlink %s %s", lnk
, path
);
98 else if (S_ISREG(st_template
.st_mode
)) {
99 if (copy_file(path
, template, st_template
.st_mode
))
100 die("cannot copy %s to %s", template, path
);
103 error("ignoring template %s", template);
107 static void copy_templates(const char *template_dir
)
110 char template_path
[PATH_MAX
];
113 const char *git_dir
= get_git_dir();
114 int len
= strlen(git_dir
);
117 template_dir
= getenv(TEMPLATE_DIR_ENVIRONMENT
);
120 * if the hard-coded template is relative, it is
121 * interpreted relative to the exec_dir
123 template_dir
= DEFAULT_GIT_TEMPLATE_DIR
;
124 if (!is_absolute_path(template_dir
)) {
125 struct strbuf d
= STRBUF_INIT
;
126 strbuf_addf(&d
, "%s/%s", git_exec_path(), template_dir
);
127 template_dir
= strbuf_detach(&d
, NULL
);
130 strcpy(template_path
, template_dir
);
131 template_len
= strlen(template_path
);
132 if (template_path
[template_len
-1] != '/') {
133 template_path
[template_len
++] = '/';
134 template_path
[template_len
] = 0;
136 dir
= opendir(template_path
);
138 fprintf(stderr
, "warning: templates not found %s\n",
143 /* Make sure that template is from the correct vintage */
144 strcpy(template_path
+ template_len
, "config");
145 repository_format_version
= 0;
146 git_config_from_file(check_repository_format_version
,
148 template_path
[template_len
] = 0;
150 if (repository_format_version
&&
151 repository_format_version
!= GIT_REPO_VERSION
) {
152 fprintf(stderr
, "warning: not copying templates of "
153 "a wrong format version %d from '%s'\n",
154 repository_format_version
,
160 memcpy(path
, git_dir
, len
);
161 if (len
&& path
[len
- 1] != '/')
164 copy_templates_1(path
, len
,
165 template_path
, template_len
,
170 static int create_default_files(const char *template_path
)
172 const char *git_dir
= get_git_dir();
173 unsigned len
= strlen(git_dir
);
174 static char path
[PATH_MAX
];
176 char repo_version_string
[10];
181 if (len
> sizeof(path
)-50)
182 die("insane git directory %s", git_dir
);
183 memcpy(path
, git_dir
, len
);
185 if (len
&& path
[len
-1] != '/')
189 * Create .git/refs/{heads,tags}
191 safe_create_dir(git_path("refs"), 1);
192 safe_create_dir(git_path("refs/heads"), 1);
193 safe_create_dir(git_path("refs/tags"), 1);
195 /* First copy the templates -- we might have the default
196 * config file there, in which case we would want to read
197 * from it after installing.
199 copy_templates(template_path
);
201 git_config(git_default_config
);
204 * We would have created the above under user's umask -- under
205 * shared-repository settings, we would need to fix them up.
207 if (shared_repository
) {
208 adjust_shared_perm(get_git_dir());
209 adjust_shared_perm(git_path("refs"));
210 adjust_shared_perm(git_path("refs/heads"));
211 adjust_shared_perm(git_path("refs/tags"));
215 * Create the default symlink from ".git/HEAD" to the "master"
216 * branch, if it does not exist yet.
218 strcpy(path
+ len
, "HEAD");
219 reinit
= (!access(path
, R_OK
)
220 || readlink(path
, junk
, sizeof(junk
)-1) != -1);
222 if (create_symref("HEAD", "refs/heads/master", NULL
) < 0)
226 /* This forces creation of new config file */
227 sprintf(repo_version_string
, "%d", GIT_REPO_VERSION
);
228 git_config_set("core.repositoryformatversion", repo_version_string
);
231 strcpy(path
+ len
, "config");
233 /* Check filemode trustability */
234 filemode
= TEST_FILEMODE
;
235 if (TEST_FILEMODE
&& !lstat(path
, &st1
)) {
237 filemode
= (!chmod(path
, st1
.st_mode
^ S_IXUSR
) &&
238 !lstat(path
, &st2
) &&
239 st1
.st_mode
!= st2
.st_mode
);
241 git_config_set("core.filemode", filemode
? "true" : "false");
243 if (is_bare_repository())
244 git_config_set("core.bare", "true");
246 const char *work_tree
= get_git_work_tree();
247 git_config_set("core.bare", "false");
248 /* allow template config file to override the default */
249 if (log_all_ref_updates
== -1)
250 git_config_set("core.logallrefupdates", "true");
251 if (prefixcmp(git_dir
, work_tree
) ||
252 strcmp(git_dir
+ strlen(work_tree
), "/.git")) {
253 git_config_set("core.worktree", work_tree
);
257 /* Check if symlink is supported in the work tree */
260 strcpy(path
+ len
, "tXXXXXX");
261 if (!close(xmkstemp(path
)) &&
263 !symlink("testing", path
) &&
264 !lstat(path
, &st1
) &&
265 S_ISLNK(st1
.st_mode
))
266 unlink(path
); /* good */
268 git_config_set("core.symlinks", "false");
274 int init_db(const char *template_dir
, unsigned int flags
)
276 const char *sha1_dir
;
280 safe_create_dir(get_git_dir(), 0);
282 /* Check to see if the repository version is right.
283 * Note that a newly created repository does not have
284 * config file, so this will not fail. What we are catching
285 * is an attempt to reinitialize new repository with an old tool.
287 check_repository_format();
289 reinit
= create_default_files(template_dir
);
291 sha1_dir
= get_object_directory();
292 len
= strlen(sha1_dir
);
293 path
= xmalloc(len
+ 40);
294 memcpy(path
, sha1_dir
, len
);
296 safe_create_dir(sha1_dir
, 1);
297 strcpy(path
+len
, "/pack");
298 safe_create_dir(path
, 1);
299 strcpy(path
+len
, "/info");
300 safe_create_dir(path
, 1);
302 if (shared_repository
) {
304 /* We do not spell "group" and such, so that
305 * the configuration can be read by older version
306 * of git. Note, we use octal numbers for new share modes,
307 * and compatibility values for PERM_GROUP and
310 if (shared_repository
== PERM_GROUP
)
311 sprintf(buf
, "%d", OLD_PERM_GROUP
);
312 else if (shared_repository
== PERM_EVERYBODY
)
313 sprintf(buf
, "%d", OLD_PERM_EVERYBODY
);
315 sprintf(buf
, "0%o", shared_repository
);
316 git_config_set("core.sharedrepository", buf
);
317 git_config_set("receive.denyNonFastforwards", "true");
320 if (!(flags
& INIT_DB_QUIET
))
321 printf("%s%s Git repository in %s/\n",
322 reinit
? "Reinitialized existing" : "Initialized empty",
323 shared_repository
? " shared" : "",
329 static int guess_repository_type(const char *git_dir
)
335 * "GIT_DIR=. git init" is always bare.
336 * "GIT_DIR=`pwd` git init" too.
338 if (!strcmp(".", git_dir
))
340 if (!getcwd(cwd
, sizeof(cwd
)))
341 die("cannot tell cwd");
342 if (!strcmp(git_dir
, cwd
))
345 * "GIT_DIR=.git or GIT_DIR=something/.git is usually not.
347 if (!strcmp(git_dir
, ".git"))
349 slash
= strrchr(git_dir
, '/');
350 if (slash
&& !strcmp(slash
, "/.git"))
354 * Otherwise it is often bare. At this point
355 * we are just guessing.
360 static const char init_db_usage
[] =
361 "git-init [-q | --quiet] [--template=<template-directory>] [--shared]";
364 * If you want to, you can share the DB area with any number of branches.
365 * That has advantages: you can save space by sharing all the SHA1 objects.
366 * On the other hand, it might just make lookup slower and messier. You
367 * be the judge. The default case is to have one DB per managed directory.
369 int cmd_init_db(int argc
, const char **argv
, const char *prefix
)
372 const char *template_dir
= NULL
;
373 unsigned int flags
= 0;
376 for (i
= 1; i
< argc
; i
++, argv
++) {
377 const char *arg
= argv
[1];
378 if (!prefixcmp(arg
, "--template="))
379 template_dir
= arg
+11;
380 else if (!strcmp(arg
, "--shared"))
381 shared_repository
= PERM_GROUP
;
382 else if (!prefixcmp(arg
, "--shared="))
383 shared_repository
= git_config_perm("arg", arg
+9);
384 else if (!strcmp(arg
, "-q") || !strcmp(arg
, "--quiet"))
385 flags
|= INIT_DB_QUIET
;
387 usage(init_db_usage
);
391 * GIT_WORK_TREE makes sense only in conjunction with GIT_DIR
392 * without --bare. Catch the error early.
394 git_dir
= getenv(GIT_DIR_ENVIRONMENT
);
395 if ((!git_dir
|| is_bare_repository_cfg
== 1)
396 && getenv(GIT_WORK_TREE_ENVIRONMENT
))
397 die("%s (or --work-tree=<directory>) not allowed without "
398 "specifying %s (or --git-dir=<directory>)",
399 GIT_WORK_TREE_ENVIRONMENT
,
400 GIT_DIR_ENVIRONMENT
);
403 * Set up the default .git directory contents
406 git_dir
= DEFAULT_GIT_DIR_ENVIRONMENT
;
408 if (is_bare_repository_cfg
< 0)
409 is_bare_repository_cfg
= guess_repository_type(git_dir
);
411 if (!is_bare_repository_cfg
) {
413 const char *git_dir_parent
= strrchr(git_dir
, '/');
414 if (git_dir_parent
) {
415 char *rel
= xstrndup(git_dir
, git_dir_parent
- git_dir
);
416 git_work_tree_cfg
= xstrdup(make_absolute_path(rel
));
420 if (!git_work_tree_cfg
) {
421 git_work_tree_cfg
= xcalloc(PATH_MAX
, 1);
422 if (!getcwd(git_work_tree_cfg
, PATH_MAX
))
423 die ("Cannot access current working directory.");
425 if (access(get_git_work_tree(), X_OK
))
426 die ("Cannot access work tree '%s'",
427 get_git_work_tree());
430 set_git_dir(make_absolute_path(git_dir
));
432 return init_db(template_dir
, flags
);